rlistdataframeenvironment-variablesnames

Error in list2env(list, envir = .GlobalEnv) : names(x) must be a character vector of the same length as x


I am trying to unlist a list of dataframes into R environment after adding a new column to each df, I would like to keep the same name for all dataframes.

My current code:

df_list <- list(CONCE84.dbf, CONCE89.dbf, CONCE92.dbf, CONCE94.dbf,
                CONCE96.dbf, CONCE98.dbf, CONCE00.dbf, CONCE02.dbf,
                CONCE04.dbf, CONCE05.dbf, CONCE06.dbf, CONCE08.dbf,
                CONCE10.dbf, CONCE12.dbf, CONCE14.dbf, CONCE16.dbf,
                CONCE18.dbf)


df_list <- lapply(df_list, function(decil) {
  
  mutate(decil,
         decil = ntile(gascor, 10))
  
})

## unlisting list to dfs ##
list2env(df_list, envir = .GlobalEnv)

## console output ##
Error in list2env(df_list, envir = .GlobalEnv) : 
  names(x) must be a character vector of the same length as x


Solution

  • As noted in the comments by Allan Cameron and improved by MrFlick, you can set the names on the fly with setNames:

    list2env(setNames(df_list, ls(pattern = "CONCE\\d\\d\\.dbf")), envir = .GlobalEnv)