I want to select rows from a data frame that have one of several values in a specified column.
My simulated data:
df <- structure(list(value = c("1", "0", "3", "2", "3", "1"),
id = c("id1", "id1", "id2", "id2", "id3", "id3"),
.Names = c("value", "id"),
row.names = c(NA, 6L),
class = "data.frame")
Which looks like:
> df
value id
1 1 id1
2 0 id1
3 3 id2
4 2 id2
5 3 id3
6 1 id3
The vector of values that should be matched is defined as:
ids <- c("id1", "id2")
Now, I know how to select rows that match one value, as discussed elsewhere on SO, namely like this:
df[df$id == "id2", ]
But I did not manage to adapt this code to go through all values contained in vector ids.