R:按时间间隔分组数据

时间:2016-12-09 09:08:19

标签: r machine-learning transactions rstudio data-science

我有以下客户交易数据框:

CustID    TransID    Product    Datetime
1         1A         Milk       19:13.7
1         1A         Bread      19:13.7
2         2A         Butter     20:15.1
2         2A         Soda       20:15.1

我正在阅读文件:

df = read.csv("data.csv", as.is = TRUE)

在这种情况下,typeof(df$Datetime)character

如何从此数据框创建三个组以反映上午,下午和晚上的交易?

基本上我想创建三个数据框:     dfMorning     dfAfternoon     dfEvening

2 个答案:

答案 0 :(得分:1)

如果早上是00:00.0到12:00等,您可以直接使用文字比较:

dfMorning <- df[df$DateTime < '12:00.0',]
dfAfternoon <- df[df$DateTime >= '12:00.0' & df$DateTime < '18:00.0',]
dfEvening <- df[df$DateTime >= '18:00.0',]

答案 1 :(得分:0)

符合Sotos建议的另一种解决方案:

breaks <- strptime(c('00:00','06:00','12:00','18:00','24:00'), '%H:%M')
labels <- c('Night', 'Morning', 'Afternoon', 'Evening')

df <- data.frame(DateTime=c('12:14.3','23:59.9','06:47.0','02:01.36'))
df$DateTime <- strptime(df$DateTime, '%H:%M')
intervals <- cut(df$DateTime, breaks=breaks, labels=labels)
res <- split(df, intervals)