Hello,
I have a data set with some column names:
> levels(down_train$CUST_REGION_DESCR)
[1] "CORPORATE REGION " "MOUNTAIN WEST REGION" "NORTH CENTRAL REGION"
[4] "NORTH EAST REGION " "OHIO VALLEY REGION " "SOUTH CENTRAL REGION "
[7] "SOUTH EAST REGION " "WESTERN REGION "
There are some white spaces within in the regions' names such as
"CORPORATE REGION "
How can get rid of the white spaces?
Thank you.
Here's one way:
library(tidyverse)
down_train <- data.frame(CUST_REGION_DESCR =
c("CORPORATE REGION ",
"MOUNTAIN WEST REGION",
"NORTH CENTRAL REGION",
"NORTH EAST REGION ",
"OHIO VALLEY REGION ",
"SOUTH CENTRAL REGION ",
"SOUTH EAST REGION ",
"WESTERN REGION "))
levels(down_train$CUST_REGION_DESCR)
[1] "CORPORATE REGION "
[2] "MOUNTAIN WEST REGION"
[3] "NORTH CENTRAL REGION"
[4] "NORTH EAST REGION "
[5] "OHIO VALLEY REGION "
[6] "SOUTH CENTRAL REGION "
[7] "SOUTH EAST REGION "
[8] "WESTERN REGION "
down_train <-
down_train %>%
dplyr::mutate(regions_no_ws =
forcats::fct_relabel(CUST_REGION_DESCR, ~ trimws(.x)))
levels(down_train$regions_no_ws)
[1] "CORPORATE REGION" "MOUNTAIN WEST REGION"
[3] "NORTH CENTRAL REGION" "NORTH EAST REGION"
[5] "OHIO VALLEY REGION" "SOUTH CENTRAL REGION"
[7] "SOUTH EAST REGION" "WESTERN REGION"
4 Likes