I am subsetting a dataframe according to multiple criteria across several columns. I am choosing the rows in the dataframe that contain any one of several values defined in the vector “criteria” in any one of three different columns.
I have some code that works, but wonder what other (more elegant?) ways there are to do this. Here is what I’ve done:
criteria <-c(1:10)
subset1 <-subset(data, data[, "Col1"] %in% criteria | data[, "Col2"]
%in% criteria | data[, "Col3"] %in% criteria)
Suggestions warmly welcomed. (I am an R beginner, so very simple explanations about what you are suggesting are also warmly welcomed.)
I’m not sure if you need two
applycalls here:Unless you want to do a lot of columns, then it is probably more readable to say: