I have a dataset that looks somewhat as follows.
data <- data.frame(
id = c(1,1,1,2,2,2,3,3,3,4,4,4),
death = c(0,0,1,0,0,0,0,1,0,0,0,0),
other = letters[1:12])
I need to create a new data frame that includes all rows with the unique IDs for any ID that has had a death, much like this:
ID | Death | Other |
---|---|---|
1 | 0 | a |
1 | 0 | b |
1 | 1 | c |
3 | 0 | g |
3 | 1 | h |
3 | 0 | i |
I feel like I'm missing something simple, but any time I try to subset by ID, I get error messages about length and not being able to subset with longer/shorter vectors. Any help would be much appreciated!