我有一个如下所示的数据集。
date <- strptime(c("2011-09-01 00:00:00","2011-09-01 06:00:00","2011-09-01 12:00:00","2011-09-01 18:00:00","2011-09-02 00:00:00",
"2011-09-02 06:00:00","2011-09-02 12:00:00","2011-09-02 18:00:00","2011-09-03 00:00:00","2011-09-03 06:00:00","2011-09-03 12:00:00",
"2011-09-03 18:00:00","2011-09-04 00:00:00","2011-09-04 06:00:00","2011-09-04 12:00:00","2011-09-04 18:00:00","2011-09-05 00:00:00",
"2011-09-05 06:00:00","2011-09-05 12:00:00","2011-09-05 18:00:00","2011-09-06 00:00:00"), format ="%Y-%m-%d %H:%M:%S")
volt <- c(7,8,9,10, 7, 8, 9, 10, 6.1, 11.1, 9.1, 10.1, 7, 8, 9, 10, 6.3, 9.4, 1.3, 19.1, 5.6)
sampV <- data.frame(date,volt)
sampV
date volt
2011-09-01 00:00:00 7
2011-09-01 06:00:00 8
2011-09-01 12:00:00 9
2011-09-01 18:00:00 10
2011-09-02 00:00:00 7
2011-09-02 06:00:00 8
2011-09-02 12:00:00 9
2011-09-02 18:00:00 10
2011-09-03 00:00:00 6.1
2011-09-03 06:00:00 11.1
2011-09-03 12:00:00 9.1
2011-09-03 18:00:00 10.1
2011-09-04 00:00:00 7
2011-09-04 06:00:00 8
2011-09-04 12:00:00 9
2011-09-04 18:00:00 10
2011-09-05 00:00:00 6.3
2011-09-05 06:00:00 9.4
2011-09-05 12:00:00 1.3
2011-09-05 18:00:00 19.1
2011-09-06 00:00:00 5.6
现在,我想使用日期列每天对数据进行分组,然后检查v中的结果分组是否重复。例如,重复第一和第二个9月的“伏特”数据(7,8,9,10)。
我一直在尝试使用此代码将其拆分为不同的日子,但这就是我可以做到的。
t1 <- strptime("2011-09-01 00:00:00",format="%Y-%m-%d %H:%M:%S")
t2 <- strptime("2011-09-06 00:00:00",format="%Y-%m-%d %H:%M:%S")
seqD <- seq(t1,t2, by="day")
ctD <- cut(sampV$date, seqD, labels=F )
spD <- split(sampV$date,ctD)
我的问题是,如何使用重复功能或任何功能将这些数据从一天复制到下一天?我只是R的初学者,我还在学习绳索,所以你的帮助将不胜感激。感谢
答案 0 :(得分:2)
假设我已正确理解您的问题,这里仅使用split
和duplicated
:
days <- format(sampV$date, '%Y%m%d')
filtered <- split(sampV, days)[! duplicated(split(sampV$volt, days))]
do.call(rbind, filtered)
# date volt
# 20110901.1 2011-09-01 00:00:00 7.0
# 20110901.2 2011-09-01 06:00:00 8.0
# 20110901.3 2011-09-01 12:00:00 9.0
# 20110901.4 2011-09-01 18:00:00 10.0
# 20110903.9 2011-09-03 00:00:00 6.1
# 20110903.10 2011-09-03 06:00:00 11.1
# 20110903.11 2011-09-03 12:00:00 9.1
# 20110903.12 2011-09-03 18:00:00 10.1
# 20110905.17 2011-09-05 00:00:00 6.3
# 20110905.18 2011-09-05 06:00:00 9.4
# 20110905.19 2011-09-05 12:00:00 1.3
# 20110905.20 2011-09-05 18:00:00 19.1
# 20110906 2011-09-06 00:00:00 5.6