Problem:
I am trying to filter my dataframe by specific datetimes that are dependent on an ID vector.
我试图通过依赖于ID向量的特定日期时间来过滤我的数据帧。
Specifically, for observations where df$id == "A"
, I want to remove rows between 2017-08-05 00:20:00
and 2017-08-10 13:55:00
. However, for observations where df$id == "B"
, I want to remove rows between a different time interval, 2017-08-05 00:30:00
and 2017-08-10 13:55:00
.
具体来说,对于df $ id ==“A”的观察,我想在2017-08-05 00:20:00和2017-08-10 13:55:00之间删除行。但是,对于df $ id ==“B”的观察,我想在不同的时间间隔2017-08-05 00:30:00和2017-08-10 13:55:00之间删除行。
Example dataframe:
date <- as.POSIXct(c("2017-08-04 16:40:00","2017-08-05 00:20:00","2017-08-10 13:55:00","2017-08-15 08:35:00", "2017-08-04 17:20:00","2017-08-05 00:30:00","2017-08-10 13:55:00","2017-08-15 09:30:00"), format = "%Y-%m-%d %H:%M:%S")
value <- as.numeric(c(1, 2, 3, 4, 1, 2, 3, 4))
id <- as.factor(c("A","A","A","A","B","B","B","B"))
df <- data.frame(date, value, id)
Desired output:
date value id
2017-08-04 16:40:00 1 A
2017-08-15 08:35:00 4 A
2017-08-04 17:20:00 1 B
2017-08-15 09:30:00 4 B
Thanks!
Edit: if your dataframe has a third category (df$id == "C"
) that you want to preserve in its entirety:
编辑:如果您的数据框具有要完整保留的第三个类别(df $ id ==“C”):
df[which(
(df$id == "A" & (df$date < "2017-08-05 00:20:00" | df$date > "2017-08-10 13:55:00")) |
(df$id == "B" & (df$date < "2017-08-05 00:30:00" | df$date > "2017-08-10 13:55:00"))
| df$id == "C"), ]
1 个解决方案
#1
2
df[which(
(df$id == "A" & (df$date < "2017-08-05 00:20:00" | df$date > "2017-08-10 13:55:00")) |
(df$id == "B" & (df$date < "2017-08-05 00:30:00" | df$date > "2017-08-10 13:55:00"))
), ]
#1
2
df[which(
(df$id == "A" & (df$date < "2017-08-05 00:20:00" | df$date > "2017-08-10 13:55:00")) |
(df$id == "B" & (df$date < "2017-08-05 00:30:00" | df$date > "2017-08-10 13:55:00"))
), ]