# Convert "ragged" list to matrix

7 messages
Open this post in threaded view
|

## Convert "ragged" list to matrix

 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 [hidden email]           [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Convert "ragged" list to matrix

 try this: > a [[1]] [1] "a" "b" "c" [[2]] [1] "d" "e" [[3]] [1] "f" "g" "h" "i" > # find max row length > rowMax <- max(sapply(a, length)) > # now create output matrix by lengthening rows > do.call(rbind, lapply(a, function(x){ +     length(x) <- rowMax +     x + }))      [,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 <[hidden email]> 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 > > [hidden email] > > > > >        [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Convert "ragged" list to matrix

 In reply to this post by Kenneth Takagi Try this: matrix(unlist(lapply(a, '[', 1:max(sapply(a, length)))), ncol = 4, byrow = TRUE) or  do.call(rbind, lapply(a, '[', 1:max(sapply(a, length)))) On Mon, Jun 22, 2009 at 8:15 PM, Kenneth Takagi <[hidden email]> 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 > > [hidden email] > > > > >        [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Convert "ragged" list to matrix

 In reply to this post by jholtman Thanks for all the feedback; I found a previous post covering this question:   https://stat.ethz.ch/pipermail/r-help/2009-February/189232.html  Problem solved!     Kenneth Takagi [hidden email]   ________________________________ From: jim holtman [mailto:[hidden email]] Sent: Monday, June 22, 2009 7:24 PM To: Kenneth Takagi Cc: [hidden email] Subject: Re: [R] Convert "ragged" list to matrix try this: > a [[1]] [1] "a" "b" "c" [[2]] [1] "d" "e" [[3]] [1] "f" "g" "h" "i" > # find max row length > rowMax <- max(sapply(a, length)) > # now create output matrix by lengthening rows > do.call(rbind, lapply(a, function(x){ +     length(x) <- rowMax +     x + }))      [,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 <[hidden email]> 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 [hidden email]        [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Convert "ragged" list to matrix

 In reply to this post by Kenneth Takagi 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<[hidden email]> 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 > > [hidden email] > > > > >        [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.