Hello R masters, This request is being made after searching through archive mailing list. I have a "many" table in a medical application where each injury a person suffers is recorded as a separate row. In my toy example below it means respondent #1 had three injuries and so on. If x2 equal 2 then I want to delete all the rows for that person from the dataframe--see Before and After below. I have thought about using match to first identify the IDs that have a 2 in x2 and then in a second step delete all rows that have matching IDs with this subset, but I have not been able to get it to work. Any help would be much appreciated. Before x1 <- c(1,1,1,2,2,3,3,3) x2 <- c(2,3,3,1,1,4,4,2) x3 <- data.frame(x1,x2)> x3x1 x2 1 1 2 2 1 3 3 1 3 4 2 1 5 2 1 6 3 4 7 3 4 8 3 2 After x1 x2 1 2 1 2 2 1 R 2.3.0 Windows XP Thanks! Greg Blevins The Market Solutions Group [[alternative HTML version deleted]]
François Pinard
2006-Apr-28 00:54 UTC
[R] deleting rows with the same ID if any meet a condition
[gblevins]>If x2 equal 2 then I want to delete all the rows for that person from >the dataframe--see Before and After below.>Before >x1 <- c(1,1,1,2,2,3,3,3) >x2 <- c(2,3,3,1,1,4,4,2) >x3 <- data.frame(x1,x2) >> x3 > x1 x2 >1 1 2 >2 1 3 >3 1 3 >4 2 1 >5 2 1 >6 3 4 >7 3 4 >8 3 2>After > x1 x2 >1 2 1 >2 2 1You might try: subset(x3, !x1 %in% x1[which(x2==2)]) -- Fran?ois Pinard http://pinard.progiciels-bpi.ca