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

Apply an `as.character()` function to a list of dataframes

So essentially I have a list of dataframes that I want to apply as.character() to.
To obtain the list of dataframes I have a list of files that I read in using a map() function and a read funtion that I created. I can’t use map_df() because there are columns that are being read in as different data types. All of the files are the same and I know that I could hard code the data types in the read function if I wanted, but I want to avoid that if I can.

At this point I throw the list of dataframes in a for loop and apply another map() function to apply the as.character() function. This final list of dataframes is then compressed using bind_rows().

All in all, this seems like an extremely convoluted process, see code below.

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


audits <- list.files()


my_reader <- function(x) {
  
 my_file <- read_xlsx(x)
  
  
}


audits <- map(audits, my_reader)

for (i in 1:length(audits)) {
  
  audits[[i]] <- map_df(audits[[i]], as.character)
  
  
}

audits <- bind_rows(audits)


Does anybody have any ideas on how I can improve this? Ideally to the point where I can do everything in a single vectorised map() function?

For reproducibility you can use two iris datasets with one of the columns datatypes changed.


iris2 <- iris

iris2[1] <- as.character(iris2[1])

my_list <- list(iris, iris2)

>Solution :

as.character works on vector whereas data.frame is a list of vectors. An option is to use across if we want only a single use of map

library(dplyr)
library(purrr)
map_dfr(my_list, ~ .x %>%
        mutate(across(everything(), as.character)))
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