![](/img/trans.png)
[英]Combine a column of dates with a column of times using chron package in R
[英]R programming using chron package - find if a value is between two times
我尝试在我的数据框“df.data”中创建一个名为“Day_period”的新列,该列采用以下值:如果“Times”列的值在“05:00:00”和“08”之间,则为“Early Morning” :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"
但上面的代码似乎没有完成这项工作。
这是一种方式。 鉴于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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.