[R] Convert "ragged" list to matrix

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jun 23 01:46:19 CEST 2009


Try this:

> unname(t(do.call(cbind, lapply(a, ts))))
     [,1] [,2] [,3] [,4]
[1,] "a"  "b"  "c"  NA
[2,] "d"  "e"  NA   NA
[3,] "f"  "g"  "h"  "i"


On Mon, Jun 22, 2009 at 7:15 PM, Kenneth Takagi<kat215 at psu.edu> wrote:
> Hi,
>
>
>
> I have a list made up of character strings with each item a different
> length (each item is between 1and 6 strings long).  Is there a way to
> convert a "ragged" list to a matrix such that each item is its own row?
> Here is a simple example:
>
>
>
> a=list();
>
> a[[1]] = c("a", "b", "c");
>
> a[[2]] = c("d", "e");
>
> a[[3]] = c("f", "g", "h", "i");
>
>
>
> I would like to convert the list to a matrix (or data frame) in this
> form, with the "missing" entries "NA" or something similar:
>
>
>
>     [,1] [,2] [,3] [,4]
>
> [1,] "a"  "b"  "c"  "NA"
>
> [2,] "d"  "e"  "NA"  "NA"
>
> [3,] "f"  "g"  "h"  "i"
>
>
>
>
>
> Any suggestions?
>
>
>
> Thanks!
>
>
>
>
>
> Ken
>
> kat215 at psu.edu
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list