Search code examples
rdataframetrim

Trim leading/trailing whitespaces from a data frame column where the column name comes as a variable


I have a dataframe where the name of the column which is to be trimmed for whitespaces is comming as a variable and I am not able to resolve the variable to point me to the column so that it can be trimmed.

salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
employee <- c('   John Doe   ','  Peter  Gynn ','   Jolie  Hope')
employ.data <- data.frame(employee, salary, startdate)

Here I try to trim employeecolumn and I have tried dplyr:

employ.data %>% mutate(employee = trimws(employee)) 

which works. However, If I say:

abc <- "employee"

and then employ.data %>% mutate(abc= trimws(abc))

It doesnt work.

I have tried using get(abc) in this function but this doesn't work either.

I understand I cant use abc as employ.data$abc when abc is a variable column name.

INITIAL DATAFRAME

employee         salary startdate     
    John Doe     21000  2010-11-01 
   Peter  Gynn   23400  2008-03-25 
    Jolie  Hope  26800  2007-03-14 

FINAL DATAFRAME

employee   salary startdate 
John Doe   21000  2010-11-01
Peter Gynn 23400  2008-03-25
Jolie Hope 26800  2007-03-14

Solution

  • Use mutate_at

    library(dplyr)
    employ.data %>% mutate_at(abc, trimws)
    
    #     employee salary  startdate
    #1    John Doe  21000 2010-11-01
    #2 Peter  Gynn  23400 2008-03-25
    #3 Jolie  Hope  26800 2007-03-14
    

    Or you can directly do, if you have only one column

    employ.data[[abc]] <- trimws(employ.data[[abc]])
    

    If there are multiple columns you can use lapply

    employ.data[abc] <- lapply(employ.data[abc], trimws)