Following on from post (How to write efficient code for importing multiple SAS files) I have used the below to load all SAS files into an R dataframe/tibble, however the label names are lost in unnest() step. How can one perform operations on the df$data list object to convert the SAS labels to column names? Is there a way to add in an sjlabelled::label_to_colnames() or haven::as_factor() prior to unnesting that acts on the df$data list?
library(tidyverse)
library(foreign)
library(haven)
df <- list.files(path = "E:/Cohortdata/Raw cohort/Nationalscreeningcohort",
full.names = TRUE,
recursive = TRUE,
pattern = "*.sas7bdat") %>%
tbl_df() %>%
mutate(data = map(value, read_sas)) %>%
unnest(data)
``
Edit found a solution incase helps anyone else
data.table::rbindlist() rowbinds the list column (df$data) of SAS files
as.data.frame() %>% label_to_colnames() converts to a dataframe and swaps the SAS labels to column names