Hello,
thank you all very much for your help. With your input and a bit more of
research in addition to that I found the answer:
I import the SPSS file without having R converted the catigorial variables
into factors.
By using the command
attr(mz1$sex,"value.labels")
I get the labels, which are still there!!
Thanks again,
Marion
Paul Bivand paul.bivand@gmail.com
19. Apr. (vor 5 Tagen)
an mich; r-help
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.
2012/4/24 Michael Bibo <Michael_Bibo@health.qld.gov.au>
> Hi Marion,
>
> I'm not clear why you would want to do this, but that's your
business. It
> seems like you're replacing meaningful labels with arbitrary ones.
>
> Anyway, I don't know of any way to keep arbitrary numerical codes for
> factors and labels. (This doesn't mean there isn't a way).
>
> The simplest way to do what you want might be to simply recode the factor
> (using function 'recode' from package 'car'):
>
> > sex <- factor(c("Male" , "Female" ,
"Male" , "Female"))
> > sex
> [1] Male Female Male Female
> Levels: Female Male
> > require(car)
> > sex_num <- recode (x, ' "Male" = -2;
"Female" = -3', as.factor.result > T)
> > sex_num
> [1] -2 -3 -2 -3
> Levels: -2 -3
> This example creates a new factor, so you have access to both. But you
> could also overwrite the original with the new factor.
>
>
> Hope this is of some help.
>
> Regards,
>
>
>
> Michael Bibo
> Research Officer
> Projects and Research Service
> West Moreton Health Service District
> Queensland Health
>
> Michael_Bibo@health.qld.gov.au
>
> Ph. +61 7 3818 4857
> P.O. Box 188
> Goodna, 4301
> Australia
>
> >>> Marion Wenty <marion.wenty@gmail.com> 19/04/2012 10:34
pm >>>
> 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@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@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.
>>
>
>
>
********************************************************************************
>
> This email, including any attachments sent with it, is confidential and
> for the sole use of the intended recipient(s). This confidentiality is not
> waived or lost, if you receive it and you are not the intended
> recipient(s), or if it is transmitted/received in error.
>
> Any unauthorised use, alteration, disclosure, distribution or review of
> this email is strictly prohibited. The information contained in this email,
> including any attachment sent with it, may be subject to a statutory duty
> of confidentiality if it relates to health service matters.
>
> If you are not the intended recipient(s), or if you have received this
> email in error, you are asked to immediately notify the sender by telephone
> collect on Australia +61 1800 198 175 or by return email. You should also
> delete this email, and any copies, from your computer system network and
> destroy any hard copies produced.
>
> If not an intended recipient of this email, you must not copy, distribute
> or take any action(s) that relies on it; any form of disclosure,
> modification, distribution and/or publication of this email is also
> prohibited.
>
> Although Queensland Health takes all reasonable steps to ensure this email
> does not contain malicious software, Queensland Health does not accept
> responsibility for the consequences if any person's computer
inadvertently
> suffers any disruption to services, loss of information, harm or is
> infected with a virus, other malicious computer programme or code that may
> occur as a consequence of receiving this email.
>
> Unless stated otherwise, this email represents only the views of the
> sender and not the views of the Queensland Government.
>
>
>
**********************************************************************************
>
>
>
[[alternative HTML version deleted]]