[R] smart way to turn a vector into a matrix

Erik Iverson eriki at ccbr.umn.edu
Fri Mar 26 20:24:58 CET 2010



anna wrote:
> Oh this is a great answer too thanks erik!
> 
> -----
> Anna Lippel

It was really the same answer as Phil.  Notice if you use a data.frame 
instead of a matrix, you don't have to recast to a 'matrix'-like form, 
and therefore the split call is a little simpler. Depending on what 
you're doing, it might or might not make sense.

initialDF <- 
data.frame(rbind(cbind(rep("A",3),seq(1,3)),cbind(rep("B",4),seq(1,4)),cbind(rep("C",3),seq(1,3))))

split(initialDF, initialDF[,1])



More information about the R-help mailing list