我有这个数据框:
dput(测试)
structure(1376661600, class = c("POSIXct", "POSIXt"), tzone = "")
如果时间大于07:00且小于13:00且日期位于M-F,我需要将此值增加一小时。
我可以使用一些包来做这件事吗?
答案 0 :(得分:1)
# A data.frame with a .POSIXct column
d <- data.frame(x = .POSIXct(0, tz="GMT") + 6:14*60*60)
d
# x
#1 1970-01-01 06:00:00
#2 1970-01-01 07:00:00
#3 1970-01-01 08:00:00
#4 1970-01-01 09:00:00
#5 1970-01-01 10:00:00
#6 1970-01-01 11:00:00
#7 1970-01-01 12:00:00
#8 1970-01-01 13:00:00
#9 1970-01-01 14:00:00
# get the hours
hour <- as.POSIXlt(d[["x"]])$hour
subsetBool <- hour > 7 & hour < 13 # a logical vector to use for subsetting
# replace subset with subset + 1 hour
d[["x"]][subsetBool] <- d[["x"]][subsetBool] + 60 * 60
d
# x
#1 1970-01-01 06:00:00
#2 1970-01-01 07:00:00
#3 1970-01-01 09:00:00
#4 1970-01-01 10:00:00
#5 1970-01-01 11:00:00
#6 1970-01-01 12:00:00
#7 1970-01-01 13:00:00
#8 1970-01-01 13:00:00
#9 1970-01-01 14:00:00