2

我有这个数据框:

输入(测试)

structure(1376661600, class = c("POSIXct", "POSIXt"), tzone = "")

如果时间大于 07:00 且小于 13:00 并且日期在 MF 中,我需要将此值增加一小时。

我可以使用某种软件包来执行此操作吗?

4

1 回答 1

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
于 2013-08-20T15:08:03.423 回答