使用chron包进行R编程 - 查找值是否介于两次之间

时间:2015-01-01 13:19:09

标签: r chron

我尝试在我的数据框“df.data”中创建一个名为“Day_period”的新列,该列采用以下值: “清晨”如果列“Times”的值在“05:00:00”和“08:59:00”之间,则使用“chron”包:

require(chron)

early.morning.start <- times("05:00:00")
early.morning.stop <- times("08:59:00")

df.data$Day_period[which(df.data$Times >= early.morning.start && df.data$Times <=          
early.morning.stop)] <- "Early Morning"

但上面的代码似乎没有完成这项工作。

1 个答案:

答案 0 :(得分:3)

这是一种方法。鉴于OP的评论,似乎在这里使用cut是一个很好的方法。由于没有可重复的示例,我创建了一个小样本来演示该功能。由于您拥有大量数据集,我认为您希望更新R并使用data.table包。如果您坚持使用旧版本的R,那么transform方法将是您的选择。

# Create a sample data
mydf <- data.frame(id = 1:7,
                   time = c("01:00:00", "05:30:00", "10:00:00",
                            "14:00:00", "17:00:00", "20:00:00", "23:00:00"),
                   stringsAsFactors = FALSE)

#  id     time
#1  1 01:00:00
#2  2 05:30:00
#3  3 10:00:00
#4  4 14:00:00
#5  5 17:00:00
#6  6 20:00:00
#7  7 23:00:00

library(chron)
library(dplyr)
library(data.table)

# Convert character to times
mydf$time <- times(mydf$time)

# Base R approach
transform(mydf,
          day_period = cut(time,
                       breaks = times(c("00:00:00", "05:00:00", "09:00:00",
                                        "13:00:00", "17:00:00", "21:00:00", "23:59:00")),
                       labels = c("Late night", "Early morning", "Late morning",
                                  "Early afternoon", "Late afternoon", "Evening")))
# dplyr approach
mutate(mydf,
       day_period = cut(time,
                        breaks = times(c("00:00:00", "05:00:00", "09:00:00",
                                         "13:00:00", "17:00:00", "21:00:00", "23:59:00")),
                        labels = c("Late night", "Early morning", "Late morning",
                                   "Early afternoon", "Late afternoon", "Evening")))

# data.table approach
setDT(mydf)[, day_period := cut(time,
                                breaks = times(c("00:00:00", "05:00:00", "09:00:00",
                                                 "13:00:00", "17:00:00", "21:00:00",
                                                 "23:59:00")),
                                labels = c("Late night", "Early morning", "Late morning",
                                           "Early afternoon", "Late afternoon", "Evening"))][]

#   id     time      day_period
#1:  1 01:00:00      Late night
#2:  2 05:30:00   Early morning
#3:  3 10:00:00    Late morning
#4:  4 14:00:00 Early afternoon
#5:  5 17:00:00 Early afternoon
#6:  6 20:00:00  Late afternoon
#7:  7 23:00:00         Evening