Re: [R] selecting data from table with timestamp

From: MacQueen, Don <macqueen1_at_llnl.gov>
Date: Wed, 11 May 2011 16:41:49 -0700


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]]
>
______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Wed 11 May 2011 - 23:45:07 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 13 May 2011 - 07:00:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive