1

I have a matrix of data with column and row names, as such:

     Col1  Col2  Col3 ...
Row1 0.1   0.2   0.4  ...
Row2 1.8   13.1  0.2  ...
Row3 8.0   9.2   9.1  ...
...  ...   ...   ...

And I have an object with multiple lists of subsets of column titles, as such:

$list1
 [1] "Col1"       "Col2"      "Col13"     "Col56"     "Col267"    "Col300"
 [7] "Col301"     

$list2
 [1] "Col4"       "Col6"      "Col7"      "Col54"     "Col199"     "Col203"

...

I would like to grab a subset of the original matrix with only the columns from each list. It seem like I can do it if I specify the column titles explicitly, e.g.:

example.matrix[,c("Col4","Col6","Col7","Col54","Col199","Col203")]

But if I make a data object matching that character vector it doesn't work:

test <- as.character(example.col_list[1])
          #^this prints to terminal as 
          #"c("Col4","Col6","Col7","Col54","Col199","Col203")" 
example.matrix[,test]

This gives an Error: subscript out of bounds. After playing around with variations on this, using the subset function, trying to find indices of the columns (using which and match functions), nothing seems to work. It all seems to hinge on not being able to interpret each list of column names as I think it should. I know I am likely missing something simple but it is difficult to find an answer on SO or Google search after long effort, Any ideas?

If it helps, I am doing this to make a figure for each of the subsets of columns and there are ~10k subsets so it is not feasible to do a manual workaround. I need to nest this is a loop or within a function somehow. Thanks!

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
tbh
  • 51
  • 1
  • 5
  • Welcome to StackOverflow! Could you please post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), perhaps using `dput` on a small test data frame? – David Robinson Aug 30 '12 at 19:06
  • 1
    `list[1]` returns a *list* not a character vector. You want `list[[1]]` instead. – Joshua Ulrich Aug 30 '12 at 19:09
  • Subsetting in this way works fine (try `m=data.frame(a=1:3, b=4:6, c=7:9); l=c("a","b"); m[, l]`), so something must be different about either your `example.matrix` or your `test` vector (we can only tell what if we can reproduce it). – David Robinson Aug 30 '12 at 19:09
  • Wow, you guys are quick on the draw. I should have tried this earlier! The double bracket notation works exactly the way I need it to. I am new to R and this is my first exposure to this notation. Thanks very much for your help! – tbh Aug 30 '12 at 19:18

1 Answers1

2

You don't want to construct an R call that gets put into "[" as a character string that would then need to get parsed and evaluated. You want to put an object that is a character vector itself. Try this:

test <- example.col_list[[1]]  # note "[[" instead of "["

example.matrix[ , test]
IRTFM
  • 258,963
  • 21
  • 364
  • 487