harini_v
2015-Mar-05 09:53 UTC
[R] Want to convert list with vectore of dis similar lengths to data frame
Hi there I am Ms. Harini and I am trying to solve a problem with the function data.frame. The problem I am trying to solve is similar to the one listed under the title Example Four in this website: http://www.r-bloggers.com/converting-a-list-to-a-data-frame/ For some reason when I try to replicate the results in my R console, I get this error: test4 <- list('Row1'=letters[1:5], 'Row2'=letters[1:7], 'Row3'=letters[8:14]) as.data.frame(test4) Error in data.frame(Row1 = c("a", "b", "c", "d", "e"), Row2 = c("a", "b", : arguments imply differing number of rows: 5, 7 It says that it can?t do what I am asking it to do because the lengths of the rows are different. But somehow it has worked on the above website. This is an old article (written in 2013). My question is this: Would you know if the way data.frame handles lists has been re written in some way? I couldn?t find anything to answer my question on the web, so I thought I should email you. Thank you for the time you are taking to read my email and trying to help me. Sincerely, Harini -- View this message in context: http://r.789695.n4.nabble.com/Want-to-convert-list-with-vectore-of-dis-similar-lengths-to-data-frame-tp4704196.html Sent from the R help mailing list archive at Nabble.com.
JS Huang
2015-Mar-05 14:04 UTC
[R] Want to convert list with vectore of dis similar lengths to data frame
Hi, The following works by appending NA to make up the maximum length of the list.> test4$Row1 [1] "a" "b" "c" "d" "e" $Row2 [1] "a" "b" "c" "d" "e" "f" "g" $Row3 [1] "h" "i" "j" "k" "l" "m" "n"> as.data.frame(t(sapply(1:length(test4), > function(x){c(test4[[x]],rep(NA,max(sapply(1:length(test4),function(x)length(test4[[x]])))-length(test4[[x]])))})))V1 V2 V3 V4 V5 V6 V7 1 a b c d e <NA> <NA> 2 a b c d e f g 3 h i j k l m n -- View this message in context: http://r.789695.n4.nabble.com/Want-to-convert-list-with-vectore-of-dis-similar-lengths-to-data-frame-tp4704196p4704200.html Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2015-Mar-05 21:03 UTC
[R] Want to convert list with vectore of dis similar lengths to data frame
That blog post refers to the authors own function, as.data.frame.list, that you would have to download from github. It makes data.frame() act quite differently than the usual way. I would not recommend it. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Mar 5, 2015 at 1:53 AM, harini_v <harini.v at positiveintegers.com> wrote:> Hi there > > I am Ms. Harini and I am trying to solve a problem with the function > data.frame. The problem I am trying to solve is similar to the one listed > under the title Example Four in this website: > http://www.r-bloggers.com/converting-a-list-to-a-data-frame/ > > For some reason when I try to replicate the results in my R console, I get > this error: > test4 <- list('Row1'=letters[1:5], 'Row2'=letters[1:7], > 'Row3'=letters[8:14]) > > as.data.frame(test4) > > Error in data.frame(Row1 = c("a", "b", "c", "d", "e"), Row2 = c("a", "b", > : > arguments imply differing number of rows: 5, 7 > > It says that it can?t do what I am asking it to do because the lengths of > the rows are different. But somehow it has worked on the above website. > > This is an old article (written in 2013). My question is this: Would you > know if the way data.frame handles lists has been re written in some way? I > couldn?t find anything to answer my question on the web, so I thought I > should email you. > > Thank you for the time you are taking to read my email and trying to help > me. > Sincerely, > Harini > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Want-to-convert-list-with-vectore-of-dis-similar-lengths-to-data-frame-tp4704196.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]