[R] read.spss (package foreign) and character columns

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Thu Aug 28 12:43:45 CEST 2003


RINNER Heinrich wrote:

> In R:
> 
>>library(foreign)
>>test <- read.spss("test.sav", to.data.frame=T)
>>test
> 
>   XCHAR
> 1 a    
> 2 ab   
> 3 abcde
> 
>>levels(test$XCHAR)
> 
> [1] "a    " "ab   " "abcde"
> 
> Shouldn't it rather be "a" "ab" "abcde" (no blanks)?

I think, that should be no problem since the blanks in XCHAR may be 
easily removed with gsub()

If you have
 > s<-c("a    ","ab   ","ab cd ")

then
 > gsub(" ","",s)
[1] "a"    "ab"   "abcd"

removes all spaces or

 > gsub(" *$","",s)
[1] "a"     "ab"    "ab cd"

removes all trailing spaces.

Hope this helps!

Thomas P.




More information about the R-help mailing list