'Creating a purrr map equivalent

I have a list of files like so:

dput(files)
list(ped_cv_outcomes_in_hospital_code_v20.csv = structure(list(
    icd_10_cm_code = c("I2101", "I2102", "I2109", "I2111", "I2119", 
    "I2121", "I2129", "I213", "I214", "I219", "I21A1", "I21A9", 
    "I220", "I221", "I222", "I228", "I229", "I200", "I25110", 
    "I25700", "I25710", "I25720", "I25730", "I25750", "I25760", 
    "I25790", "B3324", "I424", "I428", "I420", "I421 ", "I422", 
    "I423", "I425", "I427", "I429", "I6300", "I63011", "I63012", 
    "I63013", "I63019", "I6302", "I63031", "I63032", "I63033", 
    "I63039", "I6309", "I6310", "I63111", "I63112", "I63113", 
    "I63119", "I6312", "I63131", "I63132", "I63133", "I63139", 
    "I6319", "I6320", "I63211", "I63212", "I63213", "I63219", 
    "I6322", "I63231", "I63232", "I63233", "I63239", "I6329", 
    "I6330", "I63311", "I63312", "I63313", "I63319", "I63321", 
    "I63322", "I63323", "I63329", "I63331", "I63332", "I63333", 
    "I63339", "I63341", "I63342", "I63343", "I63349", "I6339", 
    "I6340", "I63411", "I63412", "I63413", "I63419", "I63421", 
    "I63422", "I63423", "I63429", "I63431", "I63432", "I63433", 
    "I63439", "I63441", "I63442", "I63443", "I63449", "I6349", 
    "I6350", "I63511", "I63512", "I63513", "I63519", "I63521", 
    "I63522", "I63523", "I63529", "I63531", "I63532", "I63533", 
    "I63539", "I63541", "I63542", "I63543", "I63549", "I6359", 
    "I636", "I638", "I6381", "I6389", "I639", "B3322", "I400", 
    "I408", "I409", "I514", "I41"), code_description = c("ST elevation (STEMI) myocardial infarction involving left main coronary artery", 
    "ST elevation (STEMI) myocardial infarction involving left anterior descending coronary artery", 
    "ST elevation (STEMI) myocardial infarction involving other coronary artery of anterior wall", 
    "ST elevation (STEMI) myocardial infarction involving right coronary artery", 
    "ST elevation (STEMI) myocardial infarction involving other coronary artery of inferior wall", 
    "ST elevation (STEMI) myocardial infarction involving left circumflex coronary artery", 
    "ST elevation (STEMI) myocardial infarction involving other sites", 
    "ST elevation (STEMI) myocardial infarction of unspecified site", 
    "Non-ST elevation (NSTEMI) myocardial infarction", "Acute myocardial infarction, unspecified", 
    "Myocardial infarction type 2", "Other myocardial infarction type", 
    "Subsequent ST elevation (STEMI) myocardial infarction of anterior wall", 
    "Subsequent ST elevation (STEMI) myocardial infarction of inferior wall", 
    "Subsequent non-ST elevation (NSTEMI) myocardial infarction", 
    "Subsequent ST elevation (STEMI) myocardial infarction of other sites", 
    "Subsequent ST elevation (STEMI) myocardial infarction of unspecified site", 
    "Unstable angina", "ASHD of native coronary artery with unstable angina ", 
    "Atherosclerosis of CAGB(s), unspecified with unstable angina ", 
    "Atherosclerosis of autologous vein CABG with unstable angina", 
    "Atherosclerosis of autologous artery CABG with unstable angina", 
    "Atherosclerosis of nonautologous biological CABG with unstable angina", 
    "Atherosclerosis of native coronary artery of transplanted heart with unstable angina", 
    "Atherosclerosis of bypass graft of coronary artery of transplanted heart with unstable angina", 
    "Atherosclerosis of other CABG with unstable angina", "Viral cardiomyopathy", 
    "Endocardial fibroelastosis (congenital cardiomyopathy)", 
    "Other cardiomyopathies (cardiomyopathy newborn)", "Dilated cardiomyopathy", 
    "Obstructive hypertrophic cardiomyopathy", "Other hypertrophic cardiomyopathy", 
    "Endomyocardial (eosinophilic) disease", "Other restrictive cardiomyopathy", 
    "Cardiomyopathy due to drug and external agent", "Cardiomyopathy, unspecified", 
    "Cerebral infarction due to thrombosis of unspecified precerebral artery", 
    "Cerebral infarction due to thrombosis of right vertebral artery", 
    "Cerebral infarction due to thrombosis of left vertebral artery", 
    "Cerebral infarction due to thrombosis of bilateral vertebral arteries", 
    "Cerebral infarction due to thrombosis of unspecified vertebral artery", 
    "Cerebral infarction due to thrombosis of basilar artery", 
    "Cerebral infarction due to thrombosis of right carotid artery", 
    "Cerebral infarction due to thrombosis of left carotid artery", 
    "Cerebral infarction due to thrombosis of bilateral carotid arteries", 
    "Cerebral infarction due to thrombosis of unspecified carotid artery", 
    "Cerebral infarction due to thrombosis of other precerebral artery", 
    "Cerebral infarction due to embolism of unspecified precerebral artery ", 
    "Cerebral infarction due to embolism of right vertebral artery", 
    "Cerebral infarction due to embolism of left vertebral artery", 
    "Cerebral infarction due to embolism of bilateral vertebral arteries", 
    "Cerebral infarction due to embolism of unspecified vertebral artery", 
    "Cerebral infarction due to embolism of basilar artery", 
    "Cerebral infarction due to embolism of right carotid artery", 
    "Cerebral infarction due to embolism of left carotid artery", 
    "Cerebral infarction due to embolism of bilateral carotid arteries", 
    "Cerebral infarction due to embolism of unspecified carotid artery", 
    "Cerebral infarction due to embolism of other precerebral artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of unspecified precerebral arteries", 
    "Cerebral infarction due to unspecified occlusion or stenosis of right vertebral artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of left vertebral artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of bilateral vertebral arteries", 
    "Cerebral infarction due to unspecified occlusion or stenosis of unspecified vertebral artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of basilar artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of right carotid arteries", 
    "Cerebral infarction due to unspecified occlusion or stenosis of left carotid arteries", 
    "Cerebral infarction due to unspecified occlusion or stenosis of bilateral carotid arteries", 
    "Cerebral infarction due to unspecified occlusion or stenosis of unspecified carotid artery", 
    "Cerebral infarction due to unspecified occlusion or stenosis of other precerebral arteries", 
    "Cerebral infarction due to thromosis of unspecified cerebral artery", 
    "Cereb infrc due to thombos of right middle cerebral artery", 
    "Cerebral infrc due to thombos of left middle cerebral artery", 
    "Cerebral infrc due to thombos of bi middle cerebral arteries", 
    "Cerebral infrc due to thombos unsp middle cerebral artery", 
    "Cerebral infrc due to thombos of right ant cerebral artery", 
    "Cerebral infrc due to thombos of left ant cerebral artery", 
    "Cerebral infrc due to thombos of bi ant cerebral arteries", 
    "Cerebral infrc due to thombos unsp anterior cerebral artery", 
    "Cerebral infrc due to thombos of right post cerebral artery", 
    "Cerebral infrc due to thombos of left post cerebral artery", 
    "Cerebral infrc to thombos of bi posterior cerebral arteries", 
    "Cerebral infrc due to thombos unsp posterior cerebral artery", 
    "Cerebral infrc due to thrombosis of right cereblr artery", 
    "Cerebral infarction due to thrombosis of left cereblr artery", 
    "Cerebral infrc to thrombosis of bilateral cereblr arteries", 
    "Cerebral infarction due to thombos unsp cerebellar artery", 
    "Cerebral infarction due to thrombosis of oth cerebral artery", 
    "Cerebral infarction due to embolism of unsp cerebral artery", 
    "Cereb infrc due to embolism of right middle cerebral artery", 
    "Cereb infrc due to embolism of left middle cerebral artery", 
    "Cerebral infrc due to embolism of bi middle cerebral art", 
    "Cereb infrc due to embolism of unsp middle cerebral artery", 
    "Cerebral infrc due to embolism of right ant cerebral artery", 
    "Cerebral infrc due to embolism of left ant cerebral artery", 
    "Cerebral infrc due to embolism of bi ant cerebral arteries", 
    "Cerebral infrc due to embolism of unsp ant cerebral artery", 
    "Cerebral infrc due to embolism of right post cerebral artery", 
    "Cerebral infrc due to embolism of left post cerebral artery", 
    "Cerebral infrc due to embolism of bi post cerebral arteries", 
    "Cerebral infrc due to embolism of unsp post cerebral artery", 
    "Cerebral infarction due to embolism of right cereblr artery", 
    "Cerebral infarction due to embolism of left cereblr artery", 
    "Cerebral infrc due to embolism of bilateral cereblr arteries", 
    "Cerebral infarction due to embolism of unsp cereblr artery", 
    "Cerebral infarction due to embolism of other cerebral artery", 
    "Cereb infrc due to unsp occls or stenos of unsp cereb artery", 
    "Cereb infrc d/t unsp occls or stenos of right mid cereb art", 
    "Cereb infrc d/t unsp occls or stenos of left mid cereb art", 
    "Cereb infrc d/t unsp occls or stenos of bi middle cereb art", 
    "Cereb infrc d/t unsp occls or stenos of unsp mid cereb art", 
    "Cereb infrc d/t unsp occls or stenos of right ant cereb art", 
    "Cereb infrc d/t unsp occls or stenos of left ant cereb art", 
    "Cereb infrc due to unsp occls or stenos of bi ant cereb art", 
    "Cereb infrc d/t unsp occls or stenos of unsp ant cereb art", 
    "Cereb infrc d/t unsp occls or stenos of right post cereb art", 
    "Cereb infrc d/t unsp occls or stenos of left post cereb art", 
    "Cereb infrc due to unsp occls or stenos of bi post cereb art", 
    "Cereb infrc d/t unsp occls or stenos of unsp post cereb art", 
    "Cereb infrc due to unsp occls or stenos of right cereblr art", 
    "Cereb infrc due to unsp occls or stenos of left cereblr art", 
    "Cereb infrc due to unsp occls or stenosis of bi cereblr art", 
    "Cereb infrc due to unsp occls or stenos of unsp cereblr art", 
    "Cereb infrc due to unsp occls or stenosis of cerebral artery", 
    "Cerebral infrc due to cerebral venous thombos, nonpyogenic", 
    "Other cerebral infarction", "Other cerebral infarction due to occlusion or stenosis of small artery", 
    "Other cerebral infarction", "Cerebral infarction, unspecified", 
    "Viral myocarditis", "Infective myocarditis", "Other acute myocarditis", 
    "Acute myocarditis, unspecified", "Myocarditis, unspecified", 
    "Myocarditis in diseases classified elsewhere"), subcategory = c("ACS", 
    "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", 
    "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", 
    "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "ACS", "Cardiomyopathy", 
    "Cardiomyopathy", "Cardiomyopathy", "Cardiomyopathy", "Cardiomyopathy", 
    "Cardiomyopathy", "Cardiomyopathy", "Cardiomyopathy", "Cardiomyopathy", 
    "Cardiomyopathy", "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "ISCHEMIC STROKE", 
    "ISCHEMIC STROKE", "ISCHEMIC STROKE", "MYOCARDITIS COVID", 
    "MYOCARDITIS COVID", "MYOCARDITIS COVID", "MYOCARDITIS COVID", 
    "MYOCARDITIS COVID", "MYOCARDITIS COVID")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -134L)))

Right now I use the following code to ensure each column is a character column and that each value in each column is "squished"

# Columns to Characters ----
for(i in 1:length(files)){
  files[[i]] <- files[[i]] %>% mutate_all(as.character)
  for(c in 1:ncol(files[[i]])){
    files[[i]][[c]] <- files[[i]][[c]] %>% str_squish()
  }
}

It works perfectly fine, just wondered how to make a purrr map equivalent.



Solution 1:[1]

We can loop over the list with map, then use mutate with across as _at/_all are deprecated.

library(purrr)
library(dplyr)
library(stringr)
files <- map(files, ~ .x %>%
    mutate(across(everything(), ~ str_squish(as.character(.x)))))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun