我有这样的数据集
a <- data.frame(var1 = c("patientA", "patientA", "patientA", "patientB", "patientB", "patientB", "patientB"),
var2 = as.Date(c("2015-01-02","2015-01-04","2015-02-02","2015-02-06","2015-01-02","2015-01-07","2015-04-02")),
var3 = c(F, T, F, F, F, T, F)
)
sequ <- rle(as.character(a$var1))
a$sequ <- sequence(sequ$lengths)
制造
> a
var1 var2 var3 sequ
1 patientA 2015-01-02 FALSE 1
2 patientA 2015-01-04 TRUE 2
3 patientA 2015-02-02 FALSE 3
4 patientB 2015-02-06 FALSE 1
5 patientB 2015-01-02 FALSE 2
6 patientB 2015-01-07 TRUE 3
7 patientB 2015-04-02 FALSE 4
我如何对这个数据集进行子集化/过滤,以便获得var3 == TRUE和var2日期值大于var3 == TRUE行的所有行(by patient,var1?我试过
subset(a, (var3 == TRUE) & (var2 > var3))
但这不会产生正确的结果集。正确的是
# var1 var2 var3 sequ
# 1 patientA 2015-01-04 TRUE 2
# 2 patientA 2015-02-02 FALSE 3
# 3 patientB 2015-02-06 FALSE 1
# 4 patientB 2015-01-07 TRUE 3
# 5 patientB 2015-04-02 FALSE 4
答案 0 :(得分:6)
您可以尝试使用data.table
。在这里,我们转换了&#39; data.frame&#39;到&#39; data.table&#39; (setDT(a)
),按&#39; var1&#39;分组,我们得到&#39; var2&#39;的逻辑索引。大于或等于相应的&#39; var2&#39; &var;&#39;&#39; var3&#39;为TRUE并为数据集.SD
设置子集。
library(data.table)
setDT(a)[,.SD[var2 >= var2[var3]], var1]
# var1 var2 var3 sequ
#1: patientA 2015-01-04 TRUE 2
#2: patientA 2015-02-02 FALSE 3
#3: patientB 2015-02-06 FALSE 1
#4: patientB 2015-01-07 TRUE 3
#5: patientB 2015-04-02 FALSE 4
使用base R
的选项(假设数据按&#39; var1&#39;排序)
a[with(a, var2>=rep(var2[var3], table(var1))),]
# var1 var2 var3 sequ
#2 patientA 2015-01-04 TRUE 2
#3 patientA 2015-02-02 FALSE 3
#4 patientB 2015-02-06 FALSE 1
#6 patientB 2015-01-07 TRUE 3
#7 patientB 2015-04-02 FALSE 4
答案 1 :(得分:4)
我添加了一个列var3
为TRUE
的日期,根据它进行过滤,然后将其删除。
library(dplyr)
a %>% group_by(var1)%>%
mutate(truedate = first(var2[var3])) %>%
filter(var2 >= truedate) %>%
select(-truedate)
# Source: local data frame [5 x 4]
# Groups: var1
# var1 var2 var3 sequ
# 1 patientA 2015-01-04 TRUE 2
# 2 patientA 2015-02-02 FALSE 3
# 3 patientB 2015-02-06 FALSE 1
# 4 patientB 2015-01-07 TRUE 3
# 5 patientB 2015-04-02 FALSE 4
答案 2 :(得分:3)
基础R解决方案:首先,不要为您的rle
/ sequ
事情烦恼。而是对数据进行排序:
a <- a[order(a$var1,a$var2),]
查找选定的行:
myrows <- tapply(
1:nrow(a),
a$var1,
function(ivec){
istar <- ivec[a$var3[ivec]]
ivec[ivec>=istar]
})
带a[unlist(myrows),]
的子集。