Deep
Deep

Reputation: 588

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

Upvotes: 3

Views: 3730

Answers (2)

william3031
william3031

Reputation: 1708

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

Upvotes: 7

Ronak Shah
Ronak Shah

Reputation: 389175

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)

Upvotes: 2

Related Questions