1

I'm currently working with several datasets in R. One of these datasets has blanks in every row before every country name such as " Afghanistan". If I now want to merge this dataset with another dataset by country name R can't match the names because of the blanks. I now want to remove these blanks before every name but not between the names: e.g. the blank between "The" and "Bahamas" should stay there and only the blanks before the name should disappear in every row. How can I do that?

Thank you very much for your reply

joran
  • 169,992
  • 32
  • 429
  • 468

1 Answers1

2

We can use trimws from base R to remove the leading/lagging spaces in each string. Suppose if the column name is "countryname", we apply the trimws on that column and assign back the output to the same column.

df1$countryname <- trimws(df1$countryname)
akrun
  • 874,273
  • 37
  • 540
  • 662