2

I have some data that was inputed by researchers with unwanted spaces so for example "Sample 1" is inputed as "Sample 1 " and it is messing with my ability to call it a factor. In python I would use strip but I can't seem to find an equivalent R function. Any ideas?

  • https://stackoverflow.com/questions/2261079/how-to-trim-leading-and-trailing-whitespace-in-r – rawr Jun 29 '17 at 18:20

1 Answers1

3

trimws from the base package may be what you are looking for.

samples = c('Sample 1', 'Sample 1 ')
trimws(samples)
[1] "Sample 1" "Sample 1"

You can also apply it over a data.frame:

dat$samples <- trimws(dat$samples)
sempervent
  • 833
  • 2
  • 11
  • 23