简体   繁体   中英

Imposing the limitation on duplicated rows

In this data set, I just want to keep all information (rows) of the specific duplicated ids with negative value of event, Any suggestion would be greatly appreciated.

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)

the result would be

  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

Assuming event as a factor variable, converting it into numeric using as.numeric and ignoring the values which is greater than zero.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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