R删除数据帧中某个时间范围内的所有行
R remove all rows within a timeframe in a dataframe
我有一个包含两列的数据框。
第一列是POSIXct,第二列是num:
date value
1 09.05.2017 10:30 0.72599362
2 09.05.2017 10:31 0.6942953
3 09.05.2017 10:32 0.6913402
4 09.05.2017 10:33 0.7219035
5 09.05.2017 10:34 0.7484892
6 09.05.2017 10:35 0.7566694
7 09.05.2017 10:36 0.7699520
8 09.05.2017 10:37 0.7863227
9 09.05.2017 10:38 0.7955254
10 09.05.2017 10:39 0.7724675
11 09.05.2017 10:40 0.7883882
12 09.05.2017 10:41 0.7975705
13 09.05.2017 10:42 0.7842776
14 09.05.2017 10:43 0.7705962
15 09.05.2017 10:44 0.7607595
16 09.05.2017 10:45 0.7658722
17 09.05.2017 10:46 0.7617003
18 09.05.2017 10:47 0.7536121
19 09.05.2017 10:48 0.7493686
...
我需要两次删除 5 分钟内的所有条目。意思是我只想每 5 分钟显示一次行。
期望的结果:
date value
1 09.05.2017 10:30 0.72599362
2 09.05.2017 10:35 0.7566694
3 09.05.2017 10:40 0.7883882
4 09.05.2017 10:45 0.7658722
中间的行应该完全删除。
我想像这样使用 cut:
dfResult <- cut(dfResult$date, "5 min")
但由于某些原因,cut 函数不会从数据框中删除条目
所以我在考虑将聚合与切割一起使用。但是aggregate总是自带sum或者mean这样的函数,这也不是我想要的
dfResult <- aggregate(. ~ cut(dfResult$date, "5 min"),
dfResult[setdiff(names(dfResult), "date")], sum)
上面的行做了它应该做的,但是使用了 sum 作为聚合函数。有没有类似简单删除中间条目的函数?
谢谢!!
如果 date
是 class POSIXct,您可以使用模数运算符 (%%
) 来 return 仅对五分钟取模的行 returns 0.
给定此数据框(仅包含可重复性 - 只要您的日期列是 POSIXct 对象,您就不需要这样做):
df <- structure(list(date = structure(c(1504632600, 1504632660, 1504632720,
1504632780, 1504632840, 1504632900, 1504632960, 1504633020, 1504633080,
1504633140, 1504633200, 1504633260, 1504633320, 1504633380, 1504633440,
1504633500, 1504633560, 1504633620, 1504633680), class = c("POSIXct",
"POSIXt"), tzone = ""), value = c(0.72599362, 0.6942953, 0.6913402,
0.7219035, 0.7484892, 0.7566694, 0.769952, 0.7863227, 0.7955254,
0.7724675, 0.7883882, 0.7975705, 0.7842776, 0.7705962, 0.7607595,
0.7658722, 0.7617003, 0.7536121, 0.7493686)), .Names = c("date",
"value"), row.names = c(NA, -19L), class = "data.frame")
Return 只有所需的行:
df[which(as.numeric(x$date) %% (60 * 5) == 0 ),]
我有一个包含两列的数据框。
第一列是POSIXct,第二列是num:
date value
1 09.05.2017 10:30 0.72599362
2 09.05.2017 10:31 0.6942953
3 09.05.2017 10:32 0.6913402
4 09.05.2017 10:33 0.7219035
5 09.05.2017 10:34 0.7484892
6 09.05.2017 10:35 0.7566694
7 09.05.2017 10:36 0.7699520
8 09.05.2017 10:37 0.7863227
9 09.05.2017 10:38 0.7955254
10 09.05.2017 10:39 0.7724675
11 09.05.2017 10:40 0.7883882
12 09.05.2017 10:41 0.7975705
13 09.05.2017 10:42 0.7842776
14 09.05.2017 10:43 0.7705962
15 09.05.2017 10:44 0.7607595
16 09.05.2017 10:45 0.7658722
17 09.05.2017 10:46 0.7617003
18 09.05.2017 10:47 0.7536121
19 09.05.2017 10:48 0.7493686
...
我需要两次删除 5 分钟内的所有条目。意思是我只想每 5 分钟显示一次行。
期望的结果:
date value
1 09.05.2017 10:30 0.72599362
2 09.05.2017 10:35 0.7566694
3 09.05.2017 10:40 0.7883882
4 09.05.2017 10:45 0.7658722
中间的行应该完全删除。
我想像这样使用 cut:
dfResult <- cut(dfResult$date, "5 min")
但由于某些原因,cut 函数不会从数据框中删除条目
所以我在考虑将聚合与切割一起使用。但是aggregate总是自带sum或者mean这样的函数,这也不是我想要的
dfResult <- aggregate(. ~ cut(dfResult$date, "5 min"),
dfResult[setdiff(names(dfResult), "date")], sum)
上面的行做了它应该做的,但是使用了 sum 作为聚合函数。有没有类似简单删除中间条目的函数?
谢谢!!
如果 date
是 class POSIXct,您可以使用模数运算符 (%%
) 来 return 仅对五分钟取模的行 returns 0.
给定此数据框(仅包含可重复性 - 只要您的日期列是 POSIXct 对象,您就不需要这样做):
df <- structure(list(date = structure(c(1504632600, 1504632660, 1504632720,
1504632780, 1504632840, 1504632900, 1504632960, 1504633020, 1504633080,
1504633140, 1504633200, 1504633260, 1504633320, 1504633380, 1504633440,
1504633500, 1504633560, 1504633620, 1504633680), class = c("POSIXct",
"POSIXt"), tzone = ""), value = c(0.72599362, 0.6942953, 0.6913402,
0.7219035, 0.7484892, 0.7566694, 0.769952, 0.7863227, 0.7955254,
0.7724675, 0.7883882, 0.7975705, 0.7842776, 0.7705962, 0.7607595,
0.7658722, 0.7617003, 0.7536121, 0.7493686)), .Names = c("date",
"value"), row.names = c(NA, -19L), class = "data.frame")
Return 只有所需的行:
df[which(as.numeric(x$date) %% (60 * 5) == 0 ),]