I have an (embarrassingly) simple question: how to filter or subset a dataframe on a single value in any column of the dataframe:
df <- data.frame(
v1 = c(1:10),
v2 = c(10:1),
v3 = c(1,3,2,9,5,6,1,2,3,9)
)
Say, I want to subset df
on those rows that, in any column, contain the value 9
. I can do this like this:
df[df[,1]==9|df[,2]==9|df[,3]==9,]
v1 v2 v3
2 2 9 3
4 4 7 9
9 9 2 3
10 10 1 9
But such a convoluted code for such a trivial task! There must be more efficient ways, either in base R
or other packages.