[R] Can anyone please tell me how to strip the white spaces from a character vector?

Marc Schwartz MSchwartz at mn.rr.com
Tue Oct 25 15:06:02 CEST 2005


On Tue, 2005-10-25 at 08:51 -0400, roger bos wrote:
> for example:
> > a$tic[1:10]
> [1] "AIR " "ABCB " "ABXA " "ACMR " "ADCT " "ADEX "
> [7] "ABM " "AFCE " "AG " "ATG "
>  Can anyone please tell me how to strip the white spaces from a$tic?
>  Thanks,
>  Roger

Roger, 

See the next to last set of examples in ?sub.

> a <- c("ABM ", "AFCE ", "AG ", "ATG ")
> a
[1] "ABM "  "AFCE " "AG "   "ATG "

> a.new <- sub(' +$', '', a)
> a.new
[1] "ABM"  "AFCE" "AG"   "ATG"


Also, if this data was generated using read.table() or one of it's
variants, note the use of the 'strip.white' argument in ?read.table.

HTH,

Marc Schwartz




More information about the R-help mailing list