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@psu.edu [[alternative HTML version deleted]]
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 <kat215@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@psu.edu > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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<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]]
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 <kat215@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@psu.edu > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
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. >
Morning all, I'm trying to read in a csv file and R is having some problems. For some reason its not 'seeing' all the columns for each row, and as such is not reading in the file. I've opened the file in EXCEL and I can't see any problems with it. All rows have the correct number of columns. The code and error messages I've used are below. I've also run 'count'fields" and have included that output too. Any help or suggestions would be much appreciated. Thanks <-read.table("RWM Shopper Tracker - RAW DATA - 22JUN09 - Copy.csv", header=TRUE, sep =",", row.names=NULL) Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 13 did not have 2264 elements> count.fields("RWM Shopper Tracker - RAW DATA - 22JUN09 - Copy.csv",sep=",") [1] 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 384 2264 2264 384 2264 2264 2264 2264 2264 2264 2264 2264 [23] 2264 2264 2264 2264 152 2264 384 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 Chris Howden Marketing Scientist For all your Analysis, Modelling, Experimental Design and Training needs (mobile) 0410 689 945 (fax / office) (+618) 8952 7878 tall.chriss at yahoo.com.au
Check for unbalanced quotes or comments. try: count.fields("RWM Shopper Tracker - RAW DATA - 22JUN09 - Copy.csv", quote='', comment.char='', sep=",") On Mon, Jun 22, 2009 at 9:38 PM, Chris Howden <tall.chriss@yahoo.com.au>wrote:> Morning all, > > I'm trying to read in a csv file and R is having some problems. For some > reason its not 'seeing' all the columns for each row, and as such is not > reading in the file. > > I've opened the file in EXCEL and I can't see any problems with it. All > rows > have the correct number of columns. > > The code and error messages I've used are below. I've also run > 'count'fields" and have included that output too. > > Any help or suggestions would be much appreciated. > > Thanks > > > <-read.table("RWM Shopper Tracker - RAW DATA - 22JUN09 - Copy.csv", > header=TRUE, sep =",", row.names=NULL) > > Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, > : > line 13 did not have 2264 elements > > > > count.fields("RWM Shopper Tracker - RAW DATA - 22JUN09 - Copy.csv", > sep=",") > [1] 2264 2264 2264 2264 2264 2264 2264 2264 2264 2264 384 2264 2264 > 384 2264 2264 2264 2264 2264 2264 2264 2264 > [23] 2264 2264 2264 2264 152 2264 384 2264 2264 2264 2264 2264 2264 > 2264 2264 2264 2264 2264 2264 2264 2264 2264 > > > Chris Howden > Marketing Scientist > For all your Analysis, Modelling, Experimental Design and Training needs > (mobile) 0410 689 945 > (fax / office) (+618) 8952 7878 > tall.chriss@yahoo.com.au > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Seemingly Similar Threads
- Deleting multiple rows based on a variable
- Parenthesis around date/time using chron?
- using chron vector with boxplot
- How to Include greek symbol in axis label?
- Can anyone suggest some r packages for Experimental Designs, specifically for choice and conjoint??? (or is intersted in helping me make 1)