简体   繁体   中英

Change half hourly data into hourly in R by removing every other row

I am stuck on a very simple problem. I have a dataset and the data interval is half hourly. I want to change the data into hourly. It can be done easily by removing every other rows or rows which has time ending on 00:30. Any help is highly appreciated.

The sample dataset is as follows:

structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0), min = c(0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 
30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 
30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 
30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L, 0L, 30L), hour = c(0L, 
0L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 
8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 
15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 
21L, 22L, 22L, 23L, 23L, 0L, 0L), mday = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), mon = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), year = c(109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 
109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 
109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 
109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 
109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L), wday = c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L), yday = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 1L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt")), year = c(2009, 2009, 2009, 
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
2009, 2009, 2009), Discharge = c(36900, 37100, 37100, 37700, 
37800, 37100, 36800, 36100, 36800, 36000, 36600, 36000, 36300, 
36100, 35800, 34500, 34800, 34400, 34200, 34100, 33800, 34800, 
35100, 34900, 34800, 35000, 34600, 34500, 34200, 34300, 34100, 
33700, 33400, 33100, 32400, 32900, 31600, 32200, 32200, 32700, 
32000, 32700, 32100, 32000, 32000, 31900, 32600, 32600, 31800, 
31900)), .Names = c("date", "year", "Discharge"), row.names = 2:51, class = "data.frame")

由于您的“日期”列是POSIXlt您可以像这样配置仅包含分钟为0的行:

x[x$date$min == 0,]

R will "recycle" elements of a vector when they're not long enough. So to get every other row, you just need this:

yourdata[c(TRUE,FALSE),]

Try

dataf <- dataf[seq(nrow(dataf)) %% 2 == 1,] 

(or ==0 -- I haven't checked to see if you need odd or even rows)

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