Hello, I have a list with n numerical components of different length (3, 4 or 5 values in each component of the list); I need to export this as a text file where each component of the list will be a row and where missing values should fill in the blanks due to the different lengths of the components of the list. I think that as a first step I should convert my list to a data frame, but this is not such a simple task to accomplish: I was thinking of the following "for" loop: X<-data.frame(1,1,1,1,1); for (i in 1:length(list)) { X[i,]<-unlist(list[[i]]); } Unfortunately, when the number of elements in the components of the list are lower than 5 (maximum), I get errors or undesired results. I also tried with rbind(), but again I couldn't manage to make it accept rows of different length. Does anybody have any suggestions? Working with lists is very nice, but I still have to learn how to transfer them to text files for external use. Thnak you, Filippo Biscarini Wageningen University [[alternative HTML version deleted]]
Hi do.call("cbind", your.list) # or rbind gives you rectangular matrix, however shorter items in list are recycled as necessary. Problem is that you need to specify how shall be shorter items handeled as it is not obvious. One possibility could be add NAs to positions where you want them, what is not so simple. But maybe somebody can give more elegant solution. Regards Petr petr.pikal at precheza.cz r-help-bounces at stat.math.ethz.ch napsal dne 03.04.2007 11:28:27:> Hello, > > I have a list with n numerical components of different length (3, 4 or 5 > values in each component of the list); I need to export this as a text > file where each component of the list will be a row and where missing > values should fill in the blanks due to the different lengths of the > components of the list. > I think that as a first step I should convert my list to a data frame, > but this is not such a simple task to accomplish: I was thinking of the > following "for" loop: > > X<-data.frame(1,1,1,1,1); > > for (i in 1:length(list)) { > > X[i,]<-unlist(list[[i]]); > > } > > Unfortunately, when the number of elements in the components of the list > are lower than 5 (maximum), I get errors or undesired results. I also > tried with rbind(), but again I couldn't manage to make it accept rows > of different length. > > Does anybody have any suggestions? Working with lists is very nice, but > I still have to learn how to transfer them to text files for external > use. > > Thnak you, > > Filippo Biscarini > Wageningen University > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Biscarini, Filippo wrote:> Dear Mark, > > If you have time you can try directly into R this excerpt from my list. >This is horrible code, and I should be shot! I would appreciate any better ways of doing this, but I think this works! l = list(c(1,2,4),c(4,5,2,1),c(3,4,6,3),c(3,1,76,4,2)) fix.it <- function(list) { max.length = 0 x = NULL for(i in list) max.length = max(max.length, length(i)) for(i in list) { d = c(i, rep(NA, max.length - length(i))) if(is.null(x)) x = rbind(unlist(d)) else x = rbind(x, unlist(d)) } return(x) } fix.it(l) It works on the data you sent me:> fix.it(A)Have fun, Mark -- Dr. Mark Wardle Specialist registrar, Neurology Cardiff, UK
try something like this: lis <- list(c(1,2,4), c(4,5,2,1), c(3,4,6,3), c(3,1,76,4,2)) ########## n.max <- max(sapply(lis, length)) val <- NA # what to fill in fill <- function(x) c(x, rep(val, n.max - length(x))) as.data.frame(do.call(rbind, lapply(lis, fill))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Biscarini, Filippo" <Filippo.Biscarini at wur.nl> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, April 03, 2007 11:28 AM Subject: [R] converting a list to a data.frame> Hello, > > I have a list with n numerical components of different length (3, 4 > or 5 > values in each component of the list); I need to export this as a > text > file where each component of the list will be a row and where > missing > values should fill in the blanks due to the different lengths of the > components of the list. > I think that as a first step I should convert my list to a data > frame, > but this is not such a simple task to accomplish: I was thinking of > the > following "for" loop: > > X<-data.frame(1,1,1,1,1); > > for (i in 1:length(list)) { > > X[i,]<-unlist(list[[i]]); > > } > > Unfortunately, when the number of elements in the components of the > list > are lower than 5 (maximum), I get errors or undesired results. I > also > tried with rbind(), but again I couldn't manage to make it accept > rows > of different length. > > Does anybody have any suggestions? Working with lists is very nice, > but > I still have to learn how to transfer them to text files for > external > use. > > Thnak you, > > Filippo Biscarini > Wageningen University > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
You can concatenate a series of NA's to match the length of your longest element. (1) exampDat is example data (2) max(rapply(exampDat,length)) is length of longest element (3) function(x,m) will do the concatenation (4) sapply() will return each list element as a column of a data frame (5) t() will transpose it so you get it in row format (and convert it to a matrix. then you can use write() or write.table() to export your file to a text file. exampDat <- list(x=1:2,y=1:3,z=1:4) mat <- t(sapply(exampDat, function(x,m) c(x,rep(NA,m-length(x))), max(rapply(exampDat,length)))) ## use write.table() Hope this helps. --- "Biscarini, Filippo" <Filippo.Biscarini at wur.nl> wrote:> Hello, > > I have a list with n numerical components of different length (3, 4 or 5 > values in each component of the list); I need to export this as a text > file where each component of the list will be a row and where missing > values should fill in the blanks due to the different lengths of the > components of the list. > I think that as a first step I should convert my list to a data frame, > but this is not such a simple task to accomplish: I was thinking of the > following "for" loop: > > X<-data.frame(1,1,1,1,1); > > for (i in 1:length(list)) { > > X[i,]<-unlist(list[[i]]); > > } > > Unfortunately, when the number of elements in the components of the list > are lower than 5 (maximum), I get errors or undesired results. I also > tried with rbind(), but again I couldn't manage to make it accept rows > of different length. > > Does anybody have any suggestions? Working with lists is very nice, but > I still have to learn how to transfer them to text files for external > use. > > Thnak you, > > Filippo Biscarini > Wageningen University > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >____________________________________________________________________________________ Don't pick lemons.
On Tue, 03-Apr-2007 at 01:55PM +0200, Dimitris Rizopoulos wrote: |> try something like this: |> |> lis <- list(c(1,2,4), c(4,5,2,1), c(3,4,6,3), c(3,1,76,4,2)) |> ########## |> n.max <- max(sapply(lis, length)) |> val <- NA # what to fill in |> fill <- function(x) c(x, rep(val, n.max - length(x))) |> as.data.frame(do.call(rbind, lapply(lis, fill))) I think that should be cbind, not rbind. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Middle minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Anon ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.