Dev P
Dev P

Reputation: 449

Not able to convert lists containing dataframe

I got a list p 326 containing dataframes as shown below,


p326 <- list(Class = structure(list(Var1 = structure(c(1L, 1L, 1L, 1L, 
1L), .Label = "B", class = "factor"), Var2 = structure(1:5, .Label = c("GLP1", 
"Insulin, Fast Acting", "Metformin/SU", "Others", "SGLT"), class = "factor"), 
    Freq = c(18.1818181818182, 9.09090909090909, 9.09090909090909, 
    36.3636363636364, 27.2727272727273)), class = "data.frame", row.names = c(NA, 
-5L)), Product = structure(list(Var1 = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "B", class = "factor"), 
    Var2 = structure(1:11, .Label = c("Product 100", "Product 102", 
    "Product 103", "Product 106", "Product 25", "Product 26", 
    "Product 28", "Product 75", "Product 87", "Product 94", "Product 99"
    ), class = "factor"), Freq = c(9.09090909090909, 9.09090909090909, 
    9.09090909090909, 9.09090909090909, 9.09090909090909, 9.09090909090909, 
    9.09090909090909, 9.09090909090909, 9.09090909090909, 9.09090909090909, 
    9.09090909090909)), class = "data.frame", row.names = c(NA, 
-11L)), Product.Family = structure(list(Var1 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "B", class = "factor"), 
    Var2 = structure(1:8, .Label = c("Product Family 19", "Product Family 20", 
    "Product Family 22", "Product Family 43", "Product Family 48", 
    "Product Family 50", "Product Family 51", "Product Family 53"
    ), class = "factor"), Freq = c(9.09090909090909, 9.09090909090909, 
    9.09090909090909, 9.09090909090909, 36.3636363636364, 9.09090909090909, 
    9.09090909090909, 9.09090909090909)), class = "data.frame", row.names = c(NA, 
-8L)), Branded.Generic = structure(list(Var1 = structure(1L, .Label = "B", class = "factor"), 
    Var2 = structure(1L, .Label = "B", class = "factor"), Freq = 100), class = "data.frame", row.names = c(NA, 
-1L)))

So when I try to convert this to dataframe like shown below, I am getting error

Error: Argument 1 can't be a list containing data frames

p326 <- bind_rows(p326, .id = 'Col')

What wrong I am doing here?

Upvotes: 2

Views: 1144

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

Based on error message it looks like the input is a nested list. We can unlist one level by using recursive = FALSE and then use bind_rows.

library(dplyr)
bind_rows(unlist(p326, recursive = FALSE), .id = 'Col')

We can reproduce the same error using data provided by @A. Suliman

p326 <- list(A=list(Branded.Generic = structure(list(Var1 = structure(1L, 
.Label = "B", class = "factor"),Var2 = structure(1L, .Label = "B",class = "factor"),
Freq = 100), class = "data.frame", row.names = c(NA, -1L)),
Branded.Generi = structure(list(Var1 = structure(1L, .Label = "B",class = "factor"),
Var2 = structure(1L, .Label = "B", class = "factor"), Freq = 100), 
class = "data.frame", row.names = c(NA,-1L))))

bind_rows(p326, .id = 'Col')

Error: Argument 1 can't be a list containing data frames

bind_rows(unlist(p326, recursive = FALSE), .id = 'Col')
#                Col Var1 Var2 Freq
#1 A.Branded.Generic    B    B  100
#2  A.Branded.Generi    B    B  100

Upvotes: 3

Related Questions