Matrix to list of matrix group by column names in R

Viewed 714

I have a matrix with 6000 columns and each column belongs to one of 100 "groups" i need. I need to convert this matrix into a list 100 smaller matrices. This is a toy example of what i have:

  mat = cbind(c(2,2,2),c(3,3,3),c(4,4,4),c(1,1,1))
  colnames(mat) = c("2018.3 1","2018.3 2","2019.1 1","2019.2 2")

so the "group" is identified by the last name of each colname, here there are 2 groups. The result I need would look like:

list(cbind(c(2,2,2),c(4,4,4)),cbind(c(3,3,3),c(1,1,1)))

I've been thinking and I think it should be something like this:

lapply(do.call(cbind,sapply(something here to find the columns in each group)))

but i haven't figure out how exactly to do it.

1 Answers
Related