Search code examples
rclassdplyrlowercase

How do I make only the character strings lowercase for every data object in the environment? OR every element in a list


I have a large number of data frame objects, and I want to apply a function that only converts the character strings to lowercase and leaves the classes of every thing else alone.

I have tried mutate_all(df,tolower) but that affected all variables/columns

I have tried mutate_at(names(select_if(df,is.character)),tolower) and this does work on an individual dataframe. But I need to do this across many data frames all at the same time. I am really bad at Iteration and loops, so if code that iterates this line of code over every object may be a solution.

Or, because I also have these dataframes in a list, I have tried

modify_depth(df,.depth = 2, mutate_at(names(select_if(,is.character)),tolower)) But that did not work. modify is in the purrr package

library(dplyr)
library(purrr)

data_frame_Rate_Table <- data.frame(Policy_Class = c("rED", "WhIte","BlUe"),
                                   Rate=c(3,9,19),"Factor_1"= 1:3, Factor_2=7:9, stringsAsFactors = FALSE)

data_frame_Policyholders <- data.frame(Policy_number  = 1:10,
                                       Policy_Class=rep(c("REd","red","wHite","BLue","bluE"),2),
                                       Risk=c(rep("HiGh",5),rep("LOW",5)), 
                                       Lapse=rep(c("Y","N"),5), stringsAsFactors = FALSE)

data_frame_list <- list(data_frame_Policyholders, data_frame_Rate_Table)

I would just like all character strings to be lowercase without affecting the other classes.


Solution

  • "and over a list of dataframes map(data_frame_list, ~mutate_if(.x,is.character,tolower)) "– AndS. 7 mins ago

    This was a comment from AndS and it is EXACTLY what I wanted to happen. Thank you AndS!

    I made this an answer instead of a comment.