Ek Esawi
2018-Dec-16 03:28 UTC
[R] Combine lists into a data frame or append them to a text file
Hi All, I have an R object that is made up of N number of lists which are all of different number of columns and rows. I want to combine the N lists into a single data frame or write (append) them into text file. I hope the question is clear and doesn?t require an example. I am hoping to accomplish this using base R functions. Below is what I tried but both gave me the same error which I do understand, I think, but I don?t know how to fix it. My R object is MyTables lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) OR for (i in 1:length(MyTables)) { write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) the error Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 51, 8, 30 Thanks--EK
Bert Gunter
2018-Dec-16 05:04 UTC
[R] Combine lists into a data frame or append them to a text file
FWIW, I had no trouble writing a test case to a file with either version of your code. As we have no idea what your data look like, I don't know how anyone can diagnose the problem. But maybe I'm wrong and someone else will recognize the issue. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Dec 15, 2018 at 7:28 PM Ek Esawi <esawiek at gmail.com> wrote:> Hi All, > > I have an R object that is made up of N number of lists which are all > of different number of columns and rows. I want to combine the N > lists into a single data frame or write (append) them into text file. > I hope the question is clear and doesn?t require an example. I am > hoping to accomplish this using base R functions. > Below is what I tried but both gave me the same error which I do > understand, I think, but I don?t know how to fix it. My R object is > MyTables > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append > TRUE )) > OR > for (i in 1:length(MyTables)) { > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > the error > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 51, 8, 30 > > Thanks--EK > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Jim Lemon
2018-Dec-16 06:03 UTC
[R] Combine lists into a data frame or append them to a text file
Hi Ek, I thought there would be a simple fix for this, but had to write a little function: fillList<-function(x) { maxrows<-max(unlist(lapply(x,length))) return(lapply(x,"[",1:maxrows)) } that fills up the rows of each list with NAs. I got the expected result with: testlist<-list(a=1:8,b=1:9,c=1:10) as.data.frame(fillList(testlist)) so: for (i in 1:length(MyTables)) { write.table(as.data.frame(fillList(MyTables[i])), file = "Temp.txt",append = TRUE,quote = TRUE) may do the job. Jim On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi <esawiek at gmail.com> wrote:> > Hi All, > > I have an R object that is made up of N number of lists which are all > of different number of columns and rows. I want to combine the N > lists into a single data frame or write (append) them into text file. > I hope the question is clear and doesn?t require an example. I am > hoping to accomplish this using base R functions. > Below is what I tried but both gave me the same error which I do > understand, I think, but I don?t know how to fix it. My R object is > MyTables > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) > OR > for (i in 1:length(MyTables)) { > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > the error > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 51, 8, 30 > > Thanks--EK > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Ek Esawi
2018-Dec-16 18:29 UTC
[R] Combine lists into a data frame or append them to a text file
I tried Jim's function and it works. But here is an example just in case. AA <- list(a=c(1,2,3,4),b = c("a","b","c")) BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) mylist <- (list(AA,BB)) lapply(mylist,function(x) write.table(x,file = test.txt)) Show Traceback Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 4, 3 On Sun, Dec 16, 2018 at 8:45 AM Ek Esawi <esawiek at gmail.com> wrote:> > Thank you Jim and Bert, > > I tried Jim's function and it works. But here is an example just in case. > > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > mylist <- (list(AA,BB)) > > lapply(mylist,function(x) write.table(x,file = test.txt)) > Show Traceback > > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 4, 3 > > On Sun, Dec 16, 2018 at 1:03 AM Jim Lemon <drjimlemon at gmail.com> wrote: > > > > Hi Ek, > > I thought there would be a simple fix for this, but had to write a > > little function: > > > > fillList<-function(x) { > > maxrows<-max(unlist(lapply(x,length))) > > return(lapply(x,"[",1:maxrows)) > > } > > > > that fills up the rows of each list with NAs. I got the expected result with: > > > > testlist<-list(a=1:8,b=1:9,c=1:10) > > as.data.frame(fillList(testlist)) > > > > so: > > > > for (i in 1:length(MyTables)) { > > write.table(as.data.frame(fillList(MyTables[i])), > > file = "Temp.txt",append = TRUE,quote = TRUE) > > > > may do the job. > > > > Jim > > > > On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi <esawiek at gmail.com> wrote: > > > > > > Hi All, > > > > > > I have an R object that is made up of N number of lists which are all > > > of different number of columns and rows. I want to combine the N > > > lists into a single data frame or write (append) them into text file. > > > I hope the question is clear and doesn?t require an example. I am > > > hoping to accomplish this using base R functions. > > > Below is what I tried but both gave me the same error which I do > > > understand, I think, but I don?t know how to fix it. My R object is > > > MyTables > > > > > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) > > > OR > > > for (i in 1:length(MyTables)) { > > > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > the error > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > > check.names = TRUE, : > > > arguments imply differing number of rows: 51, 8, 30 > > > > > > Thanks--EK > > > > > > ______________________________________________ > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > 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.
Ek Esawi
2018-Dec-17 03:10 UTC
[R] Combine lists into a data frame or append them to a text file
Hi Jim, Thanks again. Actually i changed my code where the lists are not nested. Your code works, as you said for the example, but still is not working for my lists (30). My lists have different columns and rows and several are NULL; plus there are many blank space which i suppose don't make much difference, but not sure. I will try to send an actual output.. Thanks again---EK.> for (i in 1:length(MyTables)) {+ write.table(as.data.frame(fillList(MyTables[i])), + file = "Temp.txt",append = TRUE,quote = TRUE)} Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 4, 50, 53, 8, 20 In addition: There were 20 warnings (use warnings() to see t On Sun, Dec 16, 2018 at 4:10 PM Jim Lemon <drjimlemon at gmail.com> wrote:> > Hi Ek, > Okay, you got me. I didn't write the function to handle nested lists. > If you combine your initial lists into a single level list, I think it > will work: > > fillList<-function(x) { > maxrows<-max(unlist(lapply(x,length))) > return(lapply(x,"[",1:maxrows)) > } > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > mylist <- c(AA,BB) > mydf<as.data.frame(fillList(mylist)) > > Jim > > On Mon, Dec 17, 2018 at 12:45 AM Ek Esawi <esawiek at gmail.com> wrote: > > > > Thank you Jim and Bert, > > > > I tried Jim's function and it works. But here is an example just in case. > > > > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > > mylist <- (list(AA,BB)) > > > > lapply(mylist,function(x) write.table(x,file = test.txt)) > > Show Traceback > > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > check.names = TRUE, : > > arguments imply differing number of rows: 4, 3 > > > > On Sun, Dec 16, 2018 at 1:03 AM Jim Lemon <drjimlemon at gmail.com> wrote: > > > > > > Hi Ek, > > > I thought there would be a simple fix for this, but had to write a > > > little function: > > > > > > fillList<-function(x) { > > > maxrows<-max(unlist(lapply(x,length))) > > > return(lapply(x,"[",1:maxrows)) > > > } > > > > > > that fills up the rows of each list with NAs. I got the expected result with: > > > > > > testlist<-list(a=1:8,b=1:9,c=1:10) > > > as.data.frame(fillList(testlist)) > > > > > > so: > > > > > > for (i in 1:length(MyTables)) { > > > write.table(as.data.frame(fillList(MyTables[i])), > > > file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > may do the job. > > > > > > Jim > > > > > > On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi <esawiek at gmail.com> wrote: > > > > > > > > Hi All, > > > > > > > > I have an R object that is made up of N number of lists which are all > > > > of different number of columns and rows. I want to combine the N > > > > lists into a single data frame or write (append) them into text file. > > > > I hope the question is clear and doesn?t require an example. I am > > > > hoping to accomplish this using base R functions. > > > > Below is what I tried but both gave me the same error which I do > > > > understand, I think, but I don?t know how to fix it. My R object is > > > > MyTables > > > > > > > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) > > > > OR > > > > for (i in 1:length(MyTables)) { > > > > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > > > the error > > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > > > check.names = TRUE, : > > > > arguments imply differing number of rows: 51, 8, 30 > > > > > > > > Thanks--EK > > > > > > > > ______________________________________________ > > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > > 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.