1
Country<-c(" Germany"," France", "Greece"," United-State", "Spain")
Country

There are blank spaces for example before Germany, France and United-State

[1] " Germany"      " France"       "Greece"        " United-State" "Spain"

I want to suppress all the the blank spaces before the country names, in order to have some like this:

[1] "Germany"      "France"       "Greece"        "United-State" "Spain"
Seydou GORO
  • 1,147
  • 7
  • 13

2 Answers2

2

We can use trimws from base R (no packages used)

Country <- trimws(Country)
Country
#[1] "Germany"      "France"       "Greece"       "United-State" "Spain"       

Or with sub

sub("^\\s+", "", Country)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Here is a solution using str_trim from the stringr package in order to remove whitespace from the start and end of a string.

Code:

library(stringr)

Country<-c(" Germany"," France", "Greece"," United-State", "Spain")

str_trim(Country)

Output:

[1] "Germany"      "France"       "Greece"       "United-State" "Spain"

Original:

[1] " Germany"      " France"       "Greece"        " United-State" "Spain"    

Created on 2020-11-21 by the reprex package (v0.3.0)

Eric
  • 2,699
  • 5
  • 17