2

我有一个如下所示的数据集。

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 月 1 日和 2 日的“伏特”数据 (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 的初学者,我仍在学习绳索,所以您的帮助将不胜感激。谢谢

4

1 回答 1

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
于 2012-11-08T17:13:10.547 回答