简体   繁体   中英

Remove space after characters in R?

I currently have a script that writes an Excel file and sends out an email from R. My loop fails because when the excel path is created there a space for whatever reason (see below). How can I get rid of the spaces after the vendor name? It would be great if someone can provide a dplyr solution.

Old :

Vendor_Name               
   <chr>                     
 1 "INTERLINE BRANDS"        
 2 "GENERAC SO"              
 3 "SO DIMPLEX NORTH AMERIC" 
 4 "ZEPENFORCER"             
 5 "SO TORO                " 
 6 "SO MTDARNOLD PRODUCTS "  
 7 "SO IMPERIAL INDUSTRIAL " 
 8 "SO QUICKIE             " 
 9 "SO RUBBERMAID SPECIALTY" 
10 "TIGER SUPPLIES          "

Ideal :

Vendor_Name               
   <chr>                     
 1 "INTERLINE BRANDS"        
 2 "GENERAC SO"              
 3 "SO DIMPLEX NORTH AMERIC" 
 4 "ZEPENFORCER"             
 5 "SO TORO" 
 6 "SO MTDARNOLD PRODUCTS"  
 7 "SO IMPERIAL INDUSTRIAL" 
 8 "SO QUICKIE" 
 9 "SO RUBBERMAID SPECIALTY" 
10 "TIGER SUPPLIES"

Use trimws :

trimws(x$Vendor_Name)
#  [1] "INTERLINE BRANDS"        "GENERAC SO"              "SO DIMPLEX NORTH AMERIC" "ZEPENFORCER"            
#  [5] "SO TORO"                 "SO MTDARNOLD PRODUCTS"   "SO IMPERIAL INDUSTRIAL"  "SO QUICKIE"             
#  [9] "SO RUBBERMAID SPECIALTY" "TIGER SUPPLIES"         

library(dplyt)
x %>%
  mutate(Vendor_Name = trimws(Vendor_Name))
#                Vendor_Name
# 1         INTERLINE BRANDS
# 2               GENERAC SO
# 3  SO DIMPLEX NORTH AMERIC
# 4              ZEPENFORCER
# 5                  SO TORO
# 6    SO MTDARNOLD PRODUCTS
# 7   SO IMPERIAL INDUSTRIAL
# 8               SO QUICKIE
# 9  SO RUBBERMAID SPECIALTY
# 10          TIGER SUPPLIES

Data

x <- structure(list(Vendor_Name = c("INTERLINE BRANDS", "GENERAC SO", "SO DIMPLEX NORTH AMERIC", "ZEPENFORCER", "SO TORO                ", "SO MTDARNOLD PRODUCTS ", "SO IMPERIAL INDUSTRIAL ", "SO QUICKIE             ", "SO RUBBERMAID SPECIALTY", "TIGER SUPPLIES          ")), class = "data.frame", row.names = c(NA, -10L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM