在这个数据集中,我只想保留具有负值事件的特定重复ID的所有信息(行),任何建议都将不胜感激。
ID<-c("R1","R2","R2","R3","R3","R4","R4","R4","R4","R3","R3","R3","R3","R2","R2","R2","R5","R6")
START<-c("3-4-2013","4-5-2018","4-5-2015","4-6-2011","5-5-2012","1-9-2010","23-4-1999","25-6-2011","3-6-2011","4-5-2014",
"6-6-2016","5-7-2014","7-7-1990","3-3-1998","4-4-1990","7-8-2014","22-4-1970","23-5-1984")
event<-c("2","-1","-3","3","4","-6","7","7","8","9","6","5","12","13","15","17","33","33")
df<-data.frame(ID,START,event)
结果将是
ID START event
2 R2 4-5-2018 -1
3 R2 4-5-2015 -3
6 R4 1-9-2010 -6
7 R4 23-4-1999 7
8 R4 25-6-2011 7
9 R4 3-6-2011 8
14 R2 3-3-1998 13
15 R2 4-4-1990 15
16 R2 7-8-2014 17
答案 0 :(得分:3)
假设event
为因子变量,使用as.numeric
将其转换为数字,并忽略大于零的值。
df[df$ID %in% df[which(!as.numeric(as.character(df$event)) > 0), "ID"], ]
#ID START event
#2 R2 4-5-2018 -1
#3 R2 4-5-2015 -3
#6 R4 1-9-2010 -6
#7 R4 23-4-1999 7
#8 R4 25-6-2011 7
#9 R4 3-6-2011 8
#14 R2 3-3-1998 13
#15 R2 4-4-1990 15
#16 R2 7-8-2014 17