Home > Software engineering >  extracting dataframes from list causes column name issue
extracting dataframes from list causes column name issue

Time:09-27

Based on the code and data below, the imap function converts every column name to 1. How can I use the following imap code in such a way that the column names don't change?

Code Data:

# Create a large dummy dataset
df2 = data.frame(Year = 2022, Reference_Number = seq(1,6694), c = seq(1,6694),
                  d = seq(1,6694), e = seq(1,6694), f = seq(1,6694),
                  g = seq(1,6694), h = seq(1,6694), i = seq(1,6694),
                  k = seq(1,6694), l = seq(1,6694), m = seq(1,6694),
                  n = seq(1,6694), o = seq(1,6694), p = seq(1,6694),
                  q = seq(1,6694), replace = T)

# Create a list with splitted datasets
n = 1000
lst2 = split(df2, as.integer(gl(nrow(df2), n, nrow(df2))))

# Extract dataframes 
 imap(lst2, ~ set_names(tibble(.x), .y)) %>%
        set_names(str_c("DFF", seq_along(.))) %>% 
        list2env(.GlobalEnv)

CodePudding user response:

You don't need to use imap here as the first argument of list2env, x is already a list:

list2env(setNames(lst2, paste0("DFF", names(lst2))), envir = globalenv())
  • Related