[R] Grep out columns using a list of strings

2015-05-08 Thread Kate Ignatius
Hi, I have a list of 150 strings, say, ap,: aajkss dfghjk sdfghk ... xxcvvn And I would l like to grep out these strings from column names in another file, af,. I've tried the following but none seem to work: aps - af[,grep(ap, colnames(af), value=TRUE)] aps - af[,grep(ap, colnames(af),

Re: [R] Grep out columns using a list of strings

2015-05-08 Thread Boris Steipe
How about %in% ? # preparing something that looks like I think your data looks like: ap - c(aajkss, dfghjk, sdfghk, xxcvvn) af - matrix(1:10, nrow=2) colnames(af) - c(aajkss, b, c, dfghjk, e) # doing what I think you need done: ap[ap %in% colnames(af)] Cheers, B. (PS. a reproducible example