Hello,
anna wrote:
Hello guys, I am working on a matrix which looks like this one:
initialMatrix <-
rbind(cbind(rep("A",3),seq(1,3)),cbind(rep("B",4),seq(1,4)),cbind(rep("C",3),seq(1,3)))
initialMatrix
[,1] [,2]
[1,] "A" "1"
[2,] "A" "2"
[3,] "A" "3"
[4,] "B" "1"
[5,] "B" "2"
[6,] "B" "3"
[7,] "B" "4"
[8,] "C" "1"
[9,] "C" "2"
[10,] "C" "3"
and I would like to turn it into a list that looks like that but don't know
which operation to apply on it:
myList<-list()
myList[[1]]<-cbind(rep("A",3),seq(1,3))
myList[[2]]<-cbind(rep("B",4),seq(1,4))
myList[[3]]<-cbind(rep("C",3),seq(1,3))
myList
[[1]]
[,1] [,2]
[1,] "A" "1"
[2,] "A" "2"
[3,] "A" "3"
[[2]]
[,1] [,2]
[1,] "B" "1"
[2,] "B" "2"
[3,] "B" "3"
[4,] "B" "4"
[[3]]
[,1] [,2]
[1,] "C" "1"
[2,] "C" "2"
[3,] "C" "3"
I thought about a using which and lapply but didn't get to a solution.
There is always a smart way to do it I'm sure but I'm not finding it...Can
somebody help please?
sapply(split(initialMatrix, f = initialMatrix[,1]), matrix, ncol = 2)
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.