Subset a dataframe between two time periods

2019-08-07 06:31发布

问题:

If I have an example dataframe:

Date <- c("05/12/2012 05:17:00", "05/12/2012 06:10:00", "05/12/2012 06:12:00", "05/12/2012 06:14:00", 
      "06/12/2012 05:25:00", "06/12/2012 06:55:00", "06/12/2012 06:19:00", "06/12/2012 08:00:00",
      "07/12/2012 05:00:00", "07/12/2012 05:19:00", "07/12/2012 06:04:00",
      "07/12/2012 06:59:00")
Date <- strptime(Date, "%d/%m/%Y %H:%M")
a <- sample(12)
hour <- as.numeric(format(Date, "%H"))
min <- as.numeric(format(Date, "%M")) / 60
hours_mins <- hour + min

df1 <- data.frame(Date,a,hour, min, hours_mins, stringsAsFactors = FALSE)

I wish to be able to subset my dataframe leaving only data (on any day) between the hours of 05:15 and 06:15.

I converted the hours and mins to a decimal variable, and hoped I might be able to do something like:

df1[df1$hours_mins >= '5.25' & df1$hours_mins < '6.25']

... but alas this doesn't work. Does anyone have any suggestions?

回答1:

Remove the quotes and add a comma in the end

df1[df1$hours_mins >= 5.25 & df1$hours_mins < 6.25,]



回答2:

Actually one of the advantages of the POSIXlt object is that it automatically carries critical date information.

Date1 <- strptime(c("05/12/2012 05:17:00", "05/12/2012 06:10:00", "05/12/2012 06:12:00", "05/12/2012 06:14:00", 
          "06/12/2012 05:25:00", "06/12/2012 06:55:00", "06/12/2012 06:19:00", "06/12/2012 08:00:00",
          "07/12/2012 05:00:00", "07/12/2012 05:19:00", "07/12/2012 06:04:00",
          "07/12/2012 06:59:00"), "%d/%m/%Y %H:%M")
class(Date1)
a <- sample(12)

#please note since strptime() is used Date1 contains "hour", "min" etc
df1 <- data.frame(Date1, hr=Date1$hour, min=Date1$min, cum_hrs=Date1$min/60+Date1$hour, a, stringsAsFactors = FALSE)
df1[(df1$hr + df1$min/60>= 5.25) & (df1$hr + df1$min/60< 6.25),]

Also if you want total hours (as a decimal) I added a column in the dataframe. I hope this will meet your needs.



标签: r subset