简体   繁体   中英

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. For instance, the "volt" data for 1st and 2nd Sept are repeated (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. Thanks

Assuming I've understood your question correctly, here's one way using just split and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM