r中的时间等级;此条件是<12:00:00是上午,而> 12:00:00是下午。

时间:2018-11-12 18:44:33

标签: r date

我需要创建一个作为时间列的列。此条件是<12:00:00是上午,而> 12:00:00是下午。我用

behaviour2$Shift <- cut(behaviour2$Time, include.lowest = TRUE, c(00:00:00, 12:00:00), labels = c('Mor', 'aft'))

但是有错误。

数据:

structure(list(Time = structure(c(1542013077, 1542013078, 1542013080, 1542013081, 1542013081, 1542013083, 1542013085, 1542013086, 1542013088, 1542013088), class = c("POSIXct", "POSIXt"), tzone = ""), IC4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Time", "IC4"), row.names = 142320:142329, class = "data.frame")

1 个答案:

答案 0 :(得分:0)

%p中的

format表示上午/下午:

behaviour2$Shift <- format(behaviour$Time, "%p")
head(behaviour2)

#Time IC4 Shift
#142320 2018-11-12 03:57:57   0    AM
#142321 2018-11-12 03:57:58   0    AM
#142322 2018-11-12 03:58:00   0    AM
#142323 2018-11-12 03:58:01   0    AM
#142324 2018-11-12 03:58:01   0    AM
#142325 2018-11-12 03:58:03   0    AM