rdata-manipulationreformatting

Function for reformatting multiple columns in R


I have five data frames in R and in each of them I have a column titled DOB for date of birth and another column titled StudentID as well as other variables. However, I would like a function that will reformat the DOB column in each data frame to be in this format %m/%d/%y and transforms the DOB variable into a character class variable. I would also like this same function to transform StudentID variable to numeric.

Is there a way to develop a function that accomplish the goals outlined above without affecting the other variables in the data set?

So far, I've just been doing the reformatting and converting manually with the code below:

df1$DOB<-as.Date(df1$DOB, format = "%m/%d/%y")
df1$DOB<-as.character(df1$DOB)
df1$StudentID <- as.numeric(df1$StudentID)

df2$DOB<-as.Date(df2$DOB, format = "%m/%d/%y")
df2$DOB<-as.character(df2$DOB)
df2$StudentID <- as.numeric(df2$StudentID)

df3$DOB<-as.Date(df3$DOB, format = "%m/%d/%y")
df3$DOB<-as.character(df3$DOB)
df3$StudentID <- as.numeric(df3$StudentID)

df4$DOB<-as.Date(df4$DOB, format = "%m/%d/%y")
df4$DOB<-as.character(df4$DOB)
df4$StudentID <- as.numeric(df4$StudentID)

df5$DOB<-as.Date(df5$DOB, format = "%m/%d/%y") 
df5$DOB<-as.character(df5$DOB)
df5$StudentID <- as.numeric(df5$StudentID)

Thank you for your help with this!

I haven't tried making a function for this yet because I'm not very good at making functions yet.


Solution

  • We can put the data.frames in a list and call function in a loop:

    library(dplyr)
    
    my_data_frames <- list(df1, df2, df3, df4)
    
    my_function <- function(df) {
        df %>%
        mutate(DOB = as.character(as.Date(DOB, format = "%m/%d/%y")),
               StudentId = as.numeric(StudentId))
        }
    
    
    lapply(my_data_frames, my_function)