-1

I am trying to transform a list of lists into data frames of list. The sub lists, however, have same number of columns but "different number of rows". When I run the command as.data.frame(parent_list), it generates: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows" errors. How can I convert this into a list of data frames with each frame having a different number of rows? I have tried solutions using different threads but no luck. Any help would be appreciated. Here is a sample code:

# making a list of 2 sub lists with different number of elements.
list <- list(A = c(1,2,3), B = c(1,2)) 

# trying to convert sub lists into data frames. 
list_to_dataframe <- as.data.frame(list) 
#> Error: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, 
#> : arguments imply differing number of rows: 3, 2"

Thanks!

Stats
  • 97
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. This is not a "general question." How you solve it depends on whatever functions you are trying to use. – MrFlick Feb 17 '20 at 21:58
  • included a sample code above – Stats Feb 17 '20 at 22:14
  • 2
    What's the desired output? What should the resulting data.frame look like? Data.frame columns have to have the same number of rows. Do you want to pad the short ones with missing values? Or are you trying to create a data.frame with one row and two list columns? – MrFlick Feb 17 '20 at 22:15
  • 3
    Your list doesn't contain two sub lists: it contains two vectors. Is this really what you meant? What do you mean that the sub-lists have the same number of columns? Can you show an example of that? – Allan Cameron Feb 17 '20 at 22:16
  • @MrFlick I am trying to create "separate" data frames within the parent list, instead of one data frame. Am I right in thinking that I can create separate data frames within a list (that's why differing number of rows should not matter)? Actually I am trying to run a ggplot through a loop, and it does not run through lists. So I am trying to convert sub lists in data frames and then run a loop. – Stats Feb 17 '20 at 22:30
  • You can have data.frames in a list. but how do you want to turn your vectors into data.frames? Are these meant to be data.frames with just one column? Are A and B meant to be the names of these new columns, or are they meant to be the names of the data.frame in the list? I'm still unclear on what the desired output is. if you included it in your question, then it would be more clear. Like `list <- list(A = data.frame(c(1,2,3)), B = data.frame(c(1,2)))` or `list <- list( data.frame(A=c(1,2,3)), data.frame(B=c(1,2)))` or something else? – MrFlick Feb 17 '20 at 22:33
  • @MrFlick each sub list is not just a vector of 1 column, instead every list has 5 columns and x no. if rows. Thus, basically each sub list is a data frame. When I try to find the class of a sublist, it shows: List of 1 $ :Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': X obs. of 5 variables: Thus, even though its a data frame, the class is still a list, which I want to change into a data frame. Sorry if my example was misleading earlier. Can I still convert the class of sub lists from list to a data frame (that have different number of rows)? – Stats Feb 17 '20 at 22:39
  • Well, that doesn't seem to bear any resemblance to the example you posted in the question itself. I'm sorry but I can't really help unless it's clear what's actually going on. – MrFlick Feb 17 '20 at 22:40
  • 1
    hey @Stats, you need to give more information about your problem. I think this is what happened, you had a dataframe where you did group_by and then split. you said you tried to "find the class", but in actual fact, you did str(list[1]) which gives you that output – StupidWolf Feb 17 '20 at 23:41
  • For example, df = data.frame(A=sample(1:2,10,replace=TRUE),B=runif(10)>0.5,C=runif(10)>0.5) %>% group_by(B,C)%>% split(.$A) ; str(df[1]) gives me something like yours but if you do str(df[[1]]), it shows a data.frame. Did you use "[[" instead of "[" ? – StupidWolf Feb 17 '20 at 23:43
  • Please show your expected output for the given `list`. Also `list` is a base R function so you might want to name it something else to avoid confusion. – Ronak Shah Feb 18 '20 at 00:30

1 Answers1

-1

You can simply use the rbind function to get the data frame, but as suggested above your list contains two vectors of unequal length so you will get the desired result with a warning:

list <- list(A = c(1,2,3), B = c(1,2)) 

# sub lists into data frames. 
do.call(rbind, list)

Output:

  [,1] [,2] [,3]
A    1    2    3
B    1    2    1

Warning message: In (function (..., deparse.level = 1) : number of columns of result is not a multiple of vector length (arg 2)

ashwin agrawal
  • 1,603
  • 8
  • 16