[R] smart way to turn a vector into a matrix
anna
lippelanna24 at hotmail.com
Fri Mar 26 19:58:59 CET 2010
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?
-----
Anna Lippel
--
View this message in context: http://n4.nabble.com/smart-way-to-turn-a-vector-into-a-matrix-tp1692671p1692671.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list