I have the following matrix
mat<-read.csv("mat.csv")
sel<-c(135, 211)
I would like to select the rows in ‘mat’ that correspond to ‘sel’
I do it in the following way:
subset(mat, mat$V2==c(sel))
and I get the following error:
Warning message:
In l[, 2] == c(135, 211) :
longer object length is not a multiple of shorter object length
And also it only selects one of the two.
Try this (credits go to Roland)
from
?'%in%you can read:If you have a logical vector indicating the matching, then you can use it for indexing and selecting the elements you want. In this case
mat$V2 %in% selmatches all elements ofmat$V2that are inselit will give you a logical vector, then using it inmat[row, col]you’ll get ontly those desired elements as inmat[mat$V2 %in% sel,]this means: Give all the columns for those rows which elements meeting the conditionmat$V2 %in% sel.