With the R functions grep()
and names()
, you can identify the columns of a matrix that meet some specified criteria.
Say we have the following matrix,
x<-data.frame(v1=c(1,2,3,4),v2=c(11,22,33,44),w1=c(1,2,3,4),w2=c(11,22,33,44))
To return only those columns that end with a character (e.g., the number 1) submit the R command grep(pattern=".[1]",x=names(x),value=TRUE) into the console.