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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 27 18:45:36 CEST 2003


On Wed, 27 Aug 2003, RINNER Heinrich wrote:

> Dear R users!
> 
> I am using R Version 1.7.1, Windows XP, package "foreign" (Version: 0.6-1),
> SPSS 11.5.1.
> 
> There is one thing I noticed with "read.spss", and I'd like to ask if this
> is considered to be a feature, or possibly a bug:
> When reading character columns, character strings seem to get filled with
> blanks at the end.
> 
> Simple example:
> In SPSS, create a file with one variable called "xchar" of type "A5"
> (character of length 5), and  3 values ("a", "ab", "abcde"), save it as
> "test.sav".
> 
> 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)?

You said it was a character string of length 5, not <=5.

It's easy to strip trailing blanks (?sub has several ways).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list