Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extracting dataframes from list causes column name issue

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading