tbh
tbh

Reputation: 51

How to call subsets of columns in a matrix using a character vector object

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!

Upvotes: 1

Views: 4288

Answers (1)

IRTFM
IRTFM

Reputation: 263391

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]

Upvotes: 2

Related Questions