johannes rara
2010-Nov-08 19:05 UTC
[R] How to rbind list of vectors with unequal vector lengths?
Hi, How to rbind these vectors from a list?:> l <- list(a = c(1, 2), b = c(1, 2, 3)) > l$a [1] 1 2 $b [1] 1 2 3> do.call(rbind, l)[,1] [,2] [,3] a 1 2 1 b 1 2 3 Warning message: In function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 1)>-J
Henrique Dallazuanna
2010-Nov-08 19:08 UTC
[R] How to rbind list of vectors with unequal vector lengths?
Try this: t(sapply(l, '[', 1:max(sapply(l, length)))) On Mon, Nov 8, 2010 at 5:05 PM, johannes rara <johannesraja@gmail.com>wrote:> Hi, > > How to rbind these vectors from a list?: > > > l <- list(a = c(1, 2), b = c(1, 2, 3)) > > l > $a > [1] 1 2 > > $b > [1] 1 2 3 > > > do.call(rbind, l) > [,1] [,2] [,3] > a 1 2 1 > b 1 2 3 > Warning message: > In function (..., deparse.level = 1) : > number of columns of result is not a multiple of vector length (arg 1) > > > > -J > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Erik Iverson
2010-Nov-08 19:10 UTC
[R] How to rbind list of vectors with unequal vector lengths?
What class of object / structure do you exactly want in the end? A matrix, a data.frame, a vector? johannes rara wrote:> Hi, > > How to rbind these vectors from a list?: > >> l <- list(a = c(1, 2), b = c(1, 2, 3)) >> l > $a > [1] 1 2 > > $b > [1] 1 2 3 > >> do.call(rbind, l) > [,1] [,2] [,3] > a 1 2 1 > b 1 2 3 > Warning message: > In function (..., deparse.level = 1) : > number of columns of result is not a multiple of vector length (arg 1) > > -J > > ______________________________________________ > 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.
johannes rara
2010-Nov-08 19:14 UTC
[R] How to rbind list of vectors with unequal vector lengths?
Thanks, data.frame or matrix. -J 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>:> What class of object / structure do you exactly want > in the end? ?A matrix, a data.frame, a vector? > > johannes rara wrote: >> >> Hi, >> >> How to rbind these vectors from a list?: >> >>> l <- list(a = c(1, 2), b = c(1, 2, 3)) >>> l >> >> $a >> [1] 1 2 >> >> $b >> [1] 1 2 3 >> >>> do.call(rbind, l) >> >> ?[,1] [,2] [,3] >> a ? ?1 ? ?2 ? ?1 >> b ? ?1 ? ?2 ? ?3 >> Warning message: >> In function (..., deparse.level = 1) ?: >> ?number of columns of result is not a multiple of vector length (arg 1) >> >> -J >> >> ______________________________________________ >> 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. >
Erik Iverson
2010-Nov-08 19:21 UTC
[R] How to rbind list of vectors with unequal vector lengths?
So what do you want the matrix to look like, since the number of columns will be different between the two rows? johannes rara wrote:> Thanks, data.frame or matrix. > > -J > > 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >> What class of object / structure do you exactly want >> in the end? A matrix, a data.frame, a vector? >> >> johannes rara wrote: >>> Hi, >>> >>> How to rbind these vectors from a list?: >>> >>>> l <- list(a = c(1, 2), b = c(1, 2, 3)) >>>> l >>> $a >>> [1] 1 2 >>> >>> $b >>> [1] 1 2 3 >>> >>>> do.call(rbind, l) >>> [,1] [,2] [,3] >>> a 1 2 1 >>> b 1 2 3 >>> Warning message: >>> In function (..., deparse.level = 1) : >>> number of columns of result is not a multiple of vector length (arg 1) >>> >>> -J >>> >>> ______________________________________________ >>> 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.
johannes rara
2010-Nov-08 19:26 UTC
[R] How to rbind list of vectors with unequal vector lengths?
This is the ideal result (data.frame):> resultnames X1 X2 X3 1 a 1 2 NA 2 b 1 2 3>2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>:> So what do you want the matrix to > look like, since the number of columns > will be different between the two rows? > > > > johannes rara wrote: >> >> Thanks, data.frame or matrix. >> >> -J >> >> 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >>> >>> What class of object / structure do you exactly want >>> in the end? ?A matrix, a data.frame, a vector? >>> >>> johannes rara wrote: >>>> >>>> Hi, >>>> >>>> How to rbind these vectors from a list?: >>>> >>>>> l <- list(a = c(1, 2), b = c(1, 2, 3)) >>>>> l >>>> >>>> $a >>>> [1] 1 2 >>>> >>>> $b >>>> [1] 1 2 3 >>>> >>>>> do.call(rbind, l) >>>> >>>> ?[,1] [,2] [,3] >>>> a ? ?1 ? ?2 ? ?1 >>>> b ? ?1 ? ?2 ? ?3 >>>> Warning message: >>>> In function (..., deparse.level = 1) ?: >>>> ?number of columns of result is not a multiple of vector length (arg 1) >>>> >>>> -J >>>> >>>> ______________________________________________ >>>> 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. >
Erik Iverson
2010-Nov-08 19:29 UTC
[R] How to rbind list of vectors with unequal vector lengths?
Then one solution is to use rbind.fill from the plyr package. johannes rara wrote:> This is the ideal result (data.frame): > >> result > names X1 X2 X3 > 1 a 1 2 NA > 2 b 1 2 3 > > 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >> So what do you want the matrix to >> look like, since the number of columns >> will be different between the two rows? >> >> >> >> johannes rara wrote: >>> Thanks, data.frame or matrix. >>> >>> -J >>> >>> 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >>>> What class of object / structure do you exactly want >>>> in the end? A matrix, a data.frame, a vector? >>>> >>>> johannes rara wrote: >>>>> Hi, >>>>> >>>>> How to rbind these vectors from a list?: >>>>> >>>>>> l <- list(a = c(1, 2), b = c(1, 2, 3)) >>>>>> l >>>>> $a >>>>> [1] 1 2 >>>>> >>>>> $b >>>>> [1] 1 2 3 >>>>> >>>>>> do.call(rbind, l) >>>>> [,1] [,2] [,3] >>>>> a 1 2 1 >>>>> b 1 2 3 >>>>> Warning message: >>>>> In function (..., deparse.level = 1) : >>>>> number of columns of result is not a multiple of vector length (arg 1) >>>>> >>>>> -J >>>>> >>>>> ______________________________________________ >>>>> 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.
johannes rara
2010-Nov-08 19:36 UTC
[R] How to rbind list of vectors with unequal vector lengths?
I have tried it, but it does not seem to work with vectors, only data.frames> do.call(rbind.fill, l)NULL>-J 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>:> Then one solution is to use > rbind.fill from the plyr package. > > johannes rara wrote: >> >> This is the ideal result (data.frame): >> >>> result >> >> ?names X1 X2 X3 >> 1 ? ? a ?1 ?2 NA >> 2 ? ? b ?1 ?2 ?3 >> >> 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >>> >>> So what do you want the matrix to >>> look like, since the number of columns >>> will be different between the two rows? >>> >>> >>> >>> johannes rara wrote: >>>> >>>> Thanks, data.frame or matrix. >>>> >>>> -J >>>> >>>> 2010/11/8 Erik Iverson <eriki at ccbr.umn.edu>: >>>>> >>>>> What class of object / structure do you exactly want >>>>> in the end? ?A matrix, a data.frame, a vector? >>>>> >>>>> johannes rara wrote: >>>>>> >>>>>> Hi, >>>>>> >>>>>> How to rbind these vectors from a list?: >>>>>> >>>>>>> l <- list(a = c(1, 2), b = c(1, 2, 3)) >>>>>>> l >>>>>> >>>>>> $a >>>>>> [1] 1 2 >>>>>> >>>>>> $b >>>>>> [1] 1 2 3 >>>>>> >>>>>>> do.call(rbind, l) >>>>>> >>>>>> ?[,1] [,2] [,3] >>>>>> a ? ?1 ? ?2 ? ?1 >>>>>> b ? ?1 ? ?2 ? ?3 >>>>>> Warning message: >>>>>> In function (..., deparse.level = 1) ?: >>>>>> ?number of columns of result is not a multiple of vector length (arg >>>>>> 1) >>>>>> >>>>>> -J >>>>>> >>>>>> ______________________________________________ >>>>>> 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. >