Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading