简体   繁体   English

通过删除每隔一行,将半小时数据更改为R中的每小时数据

[英]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. 可以通过删除时间结束于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. 当它们不够长时, R将“回收”向量的元素。 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) (或==0 - 我没有检查你是否需要奇数行或偶数行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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