[R] selecting data from table with timestamp

MacQueen, Don macqueen1 at llnl.gov
Thu May 12 01:41:49 CEST 2011


Try something similar to this:

## unchanged
full <- read.table("March_15.dat", sep=",",row.names=NULL,
as.is=TRUE,skip=1,header=TRUE)


## then convert TIMESTAMP to a date-time class
full$TIMESTAMP <- as.POSIXct(full$TIMESTAMP)

## now you can use subset()
atimeframe <- subset(full,
     TIMESTAMP >= as.POSIXct('2011-03-15 00:00:00') &
     TIMESTAMP <= as.POSIXct('2011-03-15 00:01:30')
     )

Hope this helps.

And a couple of comments on your example.

In an expression like you used,
   timestamp==as.character("2011-03-15 00:01:30")
just do
   timestamp=="2011-03-15 00:01:30"

"2011-03-15 00:01:30" is already a character string, so you don't have to
use

as.character() on it.

You used for(), but for() is used to
make loops, as in
  for (i in 1:10) { }
which has basically nothing to do with extracting a subset of rows from a
dataframe.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





-----Original Message-----
From: Pablo Rosado <pablojrosado at lbl.gov>
Date: Wed, 11 May 2011 14:21:00 -0700
To: "r-help at r-project.org" <r-help at r-project.org>
Subject: [R] selecting data from table with timestamp

>Hi,
>I am using read.table to get this data that has a timestamp.  The data is
>for many days, but I only want to run the code I wrote only for specific
>day/days/times.  I can´t figure out how to select a timeframe from the
>list.
>I have tried using subset() and didn't work
>
>I then used:
>* timestamp[for(timestamp==as.character("2011-03-15 00:00:00" )  |
>timestamp==as.character("2011-03-15 00:01:30"))]*
>but it gives me the list of the timestamp for that interval, but I want
>the
>complete data.table for that interval, not just the values of the
>TIMESTAMP
>column.
>
>Here is the script I use to read the table and select the timestamp
>interval:
>
>*full <- read.table("March_15.dat", sep=",",row.names=NULL,
>as.is=TRUE,skip=1,
>header=TRUE)
>minusrows <- full[-1:-2,]
>names(minusrows)
>timestamp <- minusrows[,1]
>timestamp3 <- timestamp[for(timestamp==as.character("2011-03-15 00:00:00"
>)
>| timestamp==as.character("2011-03-15 00:01:30"))]
>timestamp3*
>
>An example of what minusrows look like:
>*
>> minusrows[1:5,1:4]
>            TIMESTAMP RECORD Batt_Volt_Avg attic.air_temp_cool_north
>3 2011-03-15 00:00:00      0         13.35                     17.99
>4 2011-03-15 00:00:30      1         13.35                     18.00
>5 2011-03-15 00:01:00      2         13.35                     17.98
>6 2011-03-15 00:01:30      3         13.35                     17.99
>7 2011-03-15 00:02:00      4         13.35                     17.97*
>
>Thank You so much for your help and time,
>
>
>-- 
>*Pablo J. Rosado*, *Ph.D. Student*
>*Graduate Student Researcher Assistant*
>*University of California - Berkeley*
>
>	[[alternative HTML version deleted]]
>



More information about the R-help mailing list