0

I have a column of a large data frame containing regions sampled. However I have found that a number of the entries contain spaces following the regions name e.g. "East ". This is causing me problems for my analysis as R reads this as a different region to "East". Is there a way I can get rid of these spaces?

I have tried:

gsub("  ", "", df$Region)

but it doesn't seem to work.

Apologies for my ignorance I am new to R.

Many thanks!

Andrie
  • 176,377
  • 47
  • 447
  • 496
user3489562
  • 249
  • 1
  • 3
  • 11
  • 1
    Your code will replace only occurrences of two spaces, not single spaces. Is this intentional? – Andrie May 20 '14 at 14:45
  • I was trying to indicate a space after where the text would go. when I do: sort(unique(as.character(df$Region))) I still get ones with a space after the text. – user3489562 May 20 '14 at 14:52

1 Answers1

3
df$Region <- gsub(" +$", "", df$Region)

+ detects one or more spaces together, and $ detects the end of the string. That way you remove all spaces together, no matter how many there are, but only at the end of the string.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52