[R] Reading SPSS: underlying numerical codes

Paul Bivand paul.bivand at gmail.com
Thu Apr 19 14:55:21 CEST 2012


You didn't say that your workflow involved going backwards and
forwards between SPSS and R.

Importing from legacy formats like SPSS .sav is well developed, with
additional resources from packages Hmisc (spss.get) and memisc
(spss.system.file) with, in the latter case, support for reading
subsets of files.

If you need read/write access with amended SPSS datasets which
coworkers can continue to use existing syntax files for, you may need
to try going via ODBC routes.

This is a rare need, so not too well documented.

On 19 April 2012 13:34, Marion Wenty <marion.wenty at gmail.com> wrote:
> Dear Michael and Ista,
>
> thank you very much for your answers!
>
> Sorry, I think I wasn't clear about what I need. I will use an example to
> explain it again:
>
> I have imported the following data.frame with the following command using
> the foreign package:
>
> mz1<-read.spss("myfile.sav",to.data.frame=T)
>
>  mz1      asbhh apkz     sex
> 1 101010013    1 Female
> 2 101010013    2 Male
> 3 101010013    3 Female
> 4 101010030    1 Male
> 5 101010030    2 Female
> 6 101010043    1 Female
> 7 101010056    1 Male
> 8 101010060    1 Female
>
>
> R automatically converts the variable sex into a factor which is what I need.
>
> It also imports the levels, which is also what I need.
>
>
> In my example "Female" has got the number -2 as underlying code in
> SPSS and "Male" -3. Now I need this information preserved! If I just
> use the command as.numeric I get different numbers than in my spss
> file. Is there a way to preserve this information and maybe switch
> between seeing the numerical codes and the verbal codes, e.g.:
>
>
> mz1      asbhh apkz     sex
> 1 101010013    1     -2
> 2 101010013    2     -3
> 3 101010013    3     -2
> 4 101010030    1     -3
> 5 101010030    2     -2
> 6 101010043    1     -2
> 7 101010056    1     -3
> 8 101010060    1     -2
>
>
> Thank you very much for your help in advance,
>
> Marion
>
>
>
>
>
>
> 2012/4/13 Michael Bibo <michael_bibo at health.qld.gov.au>
>
>> Marion Wenty <marion.wenty <at> gmail.com> writes:
>>
>>
>> > I have got a question concerning the underlying numerical codes when
>> > reading an SPSS file into R.
>> >
>> > I used the package foreign and when I look at a variable I get the verbal
>> > codes.
>> >
>> > I would like to know how it is possible to get the underlying numerical
>> > codes as output, which are the same as in my SPSS file.
>> >
>>
>>
>> You don't need to import both numerical and text values;
>> R can automatically assign numerical values for
>> factors: see ?as.numeric.
>>
>> The default sorting of factor levels in R is alphabetical.
>> To change this, see the "levels" argument of the
>> factor command: ?factor
>>
>>
>> Hope this helps,
>>
>> Michael Bibo
>> Queensland Health
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list