Subset a dataframe between two time periods

微笑、不失礼 提交于 2019-12-11 10:02:01

问题


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.



来源:https://stackoverflow.com/questions/14981526/subset-a-dataframe-between-two-time-periods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!