Home > Back-end >  How do I combine a nested list of multiple data frames? Getting the following error
How do I combine a nested list of multiple data frames? Getting the following error

Time:10-24

I have eight data frames with various number of columns in each. I went through the process of data cleansing and combined all the common columns between them. This is the code I used to combine the commons columns.What I want to do now is combine all the columns in the nested lists as one then convert it to a data frame.

However, I get the following error:

Can't join on `x$Corruption` x `y$Corruption` because of
  incompatible types.
i `x$Corruption` is of type <double>>.
i `y$Corruption` is of type <character>>.

My code:

# Code used to combine common columns:
common_cols <- Reduce(intersect, list(colnames(df_2015), colnames(df_2016),colnames(df_2017),
                                 colnames(df_2018),colnames(df_2019),colnames(df_2020),
                                 colnames(df_2021),colnames(df_2022)))

files <- list(df_2015[common_cols],df_2016[common_cols], df_2017[common_cols], df_2018[common_cols],
            df_2019[common_cols],df_2020[common_cols],df_2021[common_cols],df_2022[common_cols])

files <- Reduce(full_join,files) # Trying to use this code to combine all the nested lists columns as one.

CodePudding user response:

Here is a way.

  1. Start by putting the data.frames in a list;
  2. loop through the list with lapply and if column Corruption is of class "character" coerce it to class "numeric".

The instructions that follow those two steps should output the same as the posted code but are much simpler. They show one of the reasons why it's better to have the data in one object, in this case a list.

(The other reason is not to clutter the .GlobalEnv.)

df_list <- list(df_2015, df_2016, df_2017, df_2018, 
                df_2019, df_2020, df_2021, df_2022)
df_list <- lapply(df_list, \(x) {
  if(is.character(x$Corruption)) x$Corruption <- as.numeric(x$Corruption)
  x
})

col_names <- lapply(df_list, names)
common_cols <- Reduce(intersect, col_names)

files <- lapply(df_list, `[`, common_cols)
files <- Reduce(dplyr::full_join, files)
  • Related