Exclude specific time periods in R

I start with R and try to search for data retrieval for certain periods of time, but cannot find anything.

I have a time series of continuous data, measured at an interval of 10 minutes for five months. For simplicity, the data is available in two columns as follows:

  Timestamp Temp.Diff
 02/14/2011 19:00 -0.385
 2/14/2011 19:10 -0.535
 02/14/2011 19:20 -0.484
 02/14/2011 19:30 -0.409
 02/14/2011 19:40 -0.385
 2/14/2011 19:50 -0.215

... And this continues for the next five months. I read the Timestamp column using as.POSIXct () in R.

Assuming that only certain times of the day are of interest to me (for example, from 12 to 15 hours), I would like to either exclude other hours of the day, or simply extract these 3 hours, but still the data stream is sequential (i.e. in the time row). I understand that you can easily multiply data if you know the row numbers, but since this is a much larger data set, is there any way to encode R so that it automatically recognizes the time period I'm looking for?

+6
source share
2 answers

You seem to know the basic idea, but there are simply not enough details. As you mentioned, we simply convert the timestamps to POSIX objects, and then a subset.

lubridate solution

The easiest way is probably with lubridate. First download the package:

library(lubridate) 

Then convert the timestamp:

 ##*m*onth *d*ay *y*ear _ *h*our *m*inute d = mdy_hm(dd$Timestamp) 

Then we choose what we want. In this case, I want any dates after 7:30 pm (regardless of the day):

 dd[hour(d) == 19 & minute(d) > 30 | hour(d) >= 20,] 

Base R Solution

First create an upper limit:

 lower = strptime("2/14/2011 19:30","%m/%d/%Y %H:%M") 

Then convert the timestamps to POSIX objects:

 d = strptime(dd$Timestamp, "%m/%d/%Y %H:%M") 

Finally, a few subsets of frames:

 dd[format(d,"%H:%M") > format(lower,"%H:%M"),] 

Thanks plannapus for this last part


Data for the above example:

 dd = read.table(textConnection('Timestamp Temp.Diff "2/14/2011 19:00" -0.385 "2/14/2011 19:10" -0.535 "2/14/2011 19:20" -0.484 "2/14/2011 19:30" -0.409 "2/14/2011 19:40" -0.385 "2/14/2011 19:50" -0.215'), header=TRUE) 
+6
source

You can do this easily with a time-based subset in the xts package. Assuming your data.frame is named Data :

 library(xts) x <- xts(Data$Temp.Diff, Data$Timestamp) y <- x["T12:00/T15:00"] # you need the leading zero if the hour is a single digit z <- x["T09:00/T12:00"] 
+2
source

Source: https://habr.com/ru/post/927715/


All Articles