[英]using split, cut and duplicated function to filter data in R
I have a data set like this one i've shown below.我有一个如下所示的数据集。
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
Now i'd like to group the data using the date column every day and then check if the resulting groupings in v are duplicated.现在我想每天使用日期列对数据进行分组,然后检查 v 中的结果分组是否重复。 For instance, the "volt" data for 1st and 2nd Sept are repeated (7,8,9,10).
例如,重复 9 月 1 日和 2 日的“伏特”数据 (7,8,9,10)。
I have been trying to use this code to split it into the various days but that is as far as i can go.我一直在尝试使用此代码将其拆分为不同的日子,但这是我所能做到的。
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)
SO my question is, how do you extract those data that have been copied from one day to the next using duplicated function or any function for that matter?所以我的问题是,您如何使用重复函数或任何与此相关的函数提取从一天复制到下一天的那些数据? I'm just a beginner in R and i'm still learning the ropes so your help will be greatly appreciated.
我只是 R 的初学者,我仍在学习绳索,因此将不胜感激您的帮助。 Thanks
谢谢
Assuming I've understood your question correctly, here's one way using just split
and duplicated
:假设我已经正确理解了您的问题,这是使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.