I've a list of 11,383 data frames. I need to merge them into one big data frame, but the have different columns (2,3,4 columns) so when i use rbind_all from Dplyr i get not desired result.
One way around would be to rbind data frames that have the same number of columns (the have different headers, but i don't mind about them). As i have data frames with 2,3 and 4 columns, it would result in 3 big data frames according to the number of columns of every single data frame in the list.
Expected Output:
Data frame with 4 columns:
SKU Tv y Video Tecnología Deportes
2003091070002P Tv y Video Tecnología Deportes
2.00E+12 Tv y Video Tecnología Deportes
2003120060008P Tv y Video Tecnología Deportes
2004121460080P Cómputo Tecnología Decohogar
2.00G+12 Cómputo Tecnología Decohogar
2004121440802P Cómputo Tecnología Decohogar
2.00A+12 Cómputo Tecnología Decohogar
Data frame with 2 columns:
SKU PROMOCIONES
1 110 2089060010006P PROMOCIONES
2 111 2089660010006P PROMOCIONES
#
This is my code:
df_2col <- data.frame() #Starts Data frame for dfs with 2 columns
df_3col <- data.frame() #Starts Data frame for dfs with 3 columns
df_4col <- data.frame() #Starts Data frame for dfs with 4 columns
lapply(my_list, function(i){
if (ncol(i) == 2)
df_2col <- rbind(i)
ifelse (ncol(i) == 3)
df_3col <- rbind(i)
ifelse (ncol(i) == 4)
df_4col <- rbind(i)
})
But i get this error:
Error in ifelse(ncol(i) == 3) : argument "no" is missing, with no default
A list example of my data:
list(list(structure(list(SKU = "2079230130006P", Decohogar = "Decohogar",
Para.la.Mesa = "Para.la.Mesa", Copas.y.Vasos = "Copas.y.Vasos"), .Names = c("SKU",
"Decohogar", "Para.la.Mesa", "Copas.y.Vasos"), class = "data.frame", row.names = 134L)),
list(structure(list(SKU = "2079240080001P", Decohogar = "Decohogar",
Para.la.Mesa = "Para.la.Mesa", Copas.y.Vasos = "Copas.y.Vasos"), .Names = c("SKU",
"Decohogar", "Para.la.Mesa", "Copas.y.Vasos"), class = "data.frame", row.names = 132L)),
list(structure(list(SKU = "2069060020005P", PROMOCIONES = "PROMOCIONES"), .Names = c("SKU",
"PROMOCIONES"), class = "data.frame", row.names = 111L)),
list(structure(list(SKU = "2047121452095P", Dormitorio = "Dormitorio",
Colchones = "Colchones", X2.plazas = "X2.plazas"), .Names = c("SKU",
"Dormitorio", "Colchones", "X2.plazas"), class = "data.frame", row.names = 223L)),
list(structure(list(SKU = "2069060010006P", PROMOCIONES = "PROMOCIONES"), .Names = c("SKU",
"PROMOCIONES"), class = "data.frame", row.names = 110L)),
list(structure(list(SKU = "2069060010006P", PROMOCIONES = "PROMOCIONES"), .Names = c("SKU",
"PROMOCIONES"), class = "data.frame", row.names = 109L)))
NOTE: This would work when you know the number of columns for every single data frame in the list. Is there a way of doing this dinamycaly? I mean, if in the future, there is a data frame with 5 columns, the code should return also a big data frame with 5 columns for this data frames.