3

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
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Deep
  • 528
  • 3
  • 12
  • 27
  • 1
    Possible duplicate of [How to use dplyr programming syntax to create and evaluate variable names](https://stackoverflow.com/questions/53859015/how-to-use-dplyr-programming-syntax-to-create-and-evaluate-variable-names) – NelsonGon May 04 '19 at 11:29

2 Answers2

7

You can also use str_trim from stringr in the tidyverse.

employ.data %>% 
  mutate(abc = str_trim(employee))

Which is:

        employee salary  startdate         abc
1    John Doe     21000 2010-11-01    John Doe
2   Peter  Gynn   23400 2008-03-25 Peter  Gynn
3    Jolie  Hope  26800 2007-03-14 Jolie  Hope
william3031
  • 1,653
  • 1
  • 18
  • 39
2

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)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213