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.
Anna -
Does this do what you want?
> parts = split(initialMatrix,initialMatrix[,1])
> parts = lapply(parts,function(x)matrix(x,ncol=2))
> parts
$A
[,1] [,2]
[1,] "A" "1"
[2,] "A" "2"
[3,] "A" "3"
$B
[,1] [,2]
[1,] "B" "1"
[2,] "B" "2"
[3,] "B" "3"
[4,] "B" "4"
$C
[,1] [,2]
[1,] "C" "1"
[2,] "C" "2"
[3,] "C" "3"
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 26 Mar 2010, 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?
>
>
> -----
> 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.
>
> ______________________________________________
> R-help at 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.
>
I tried to use ave() as well but it didn't work either... ----- Anna Lippel -- View this message in context: http://n4.nabble.com/smart-way-to-turn-a-vector-into-a-matrix-tp1692671p1692685.html Sent from the R help mailing list archive at Nabble.com.
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)
Oh this is a great answer too thanks erik! ----- Anna Lippel -- View this message in context: http://n4.nabble.com/smart-way-to-turn-a-vector-into-a-matrix-tp1692671p1692711.html Sent from the R help mailing list archive at Nabble.com.
so now the problem is that my real matrix is a data.frame.. The first column is character, second dates and third numeric. What I did is I converted the data frame into a matrix with data.matrix() but it converts my characters and dates in integers. Any idea of how I could handle this? ----- Anna Lippel -- View this message in context: http://n4.nabble.com/smart-way-to-turn-a-vector-into-a-matrix-tp1692671p1692746.html Sent from the R help mailing list archive at Nabble.com.