Re: [R] Reading SPSS: underlying numerical codes
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.biv...@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 > 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_b...@health.qld.gov.au > > Ph. +61 7 3818 4857 > P.O. Box 188 > Goodna, 4301 > Australia > > >>> Marion Wenty 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 1010100131 Female > 2 1010100132 Male > 3 1010100133 Female > 4 1010100301 Male > 5 1010100302 Female > 6 1010100431 Female > 7 1010100561 Male > 8 1010100601 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 1010100131 -2 > 2 1010100132 -3 > 3 1010100133 -2 > 4 1010100301 -3 > 5 1010100302 -2 > 6 1010100431 -2 > 7 1010100561 -3 > 8 1010100601 -2 > > > Thank you very much for your help in advance, > > Marion > > > > > > > 2012/4/13 Michael Bibo > >> Marion Wenty 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 st
Re: [R] Reading SPSS: underlying numerical codes
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 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 > >> Marion Wenty 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. >> > > [[alternative HTML version deleted]] > > __ > 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. __ 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.
Re: [R] Reading SPSS: underlying numerical codes
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 1010100131 Female 2 1010100132 Male 3 1010100133 Female 4 1010100301 Male 5 1010100302 Female 6 1010100431 Female 7 1010100561 Male 8 1010100601 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 1010100131 -2 2 1010100132 -3 3 1010100133 -2 4 1010100301 -3 5 1010100302 -2 6 1010100431 -2 7 1010100561 -3 8 1010100601 -2 Thank you very much for your help in advance, Marion 2012/4/13 Michael Bibo > Marion Wenty 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. > [[alternative HTML version deleted]] __ 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.
Re: [R] Reading SPSS: underlying numerical codes
Marion Wenty 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.
Re: [R] Reading SPSS: underlying numerical codes
Hi Marion, I don't have access to SPSS, so it would be hard for me to figure this out for you. I would think that string variables would be imported as strings or factors even when use.value.labels is true, but can't verify that. Good luck, Ista On Thu, Apr 12, 2012 at 7:59 AM, Marion Wenty wrote: > Hi Ista, > > thank you for your reply! > > Yes, I looked at the help sheet but didn't use this command because I would > still like the categorial variables to be converted into factors and have > the value labels from SPSS imported into R as levels. > > With the > > use.value.labels = FALSE? > > command I do get the same underlying codes as in SPSS but I loose the > information about the levels, I think. > > Does anyone know a way to preserve both? > > Thank you very much for your help in advance? > > Marion > > > 2012/4/12 Ista Zahn >> >> Hi Marion, >> >> Did you look at the help file? Did you try use.value.labels = FALSE? >> >> Best, >> Ista >> >> On Thu, Apr 12, 2012 at 6:28 AM, Marion Wenty >> wrote: >> > Dear people, >> > >> > 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. >> > >> > Thank you very much in advance for your help! >> > >> > Marion >> > >> > [[alternative HTML version deleted]] >> > >> > __ >> > 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. > > __ 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.
Re: [R] Reading SPSS: underlying numerical codes
Hi Ista, thank you for your reply! Yes, I looked at the help sheet but didn't use this command because I would still like the categorial variables to be converted into factors and have the value labels from SPSS imported into R as levels. With the use.value.labels = FALSE? command I do get the same underlying codes as in SPSS but I loose the information about the levels, I think. Does anyone know a way to preserve both? Thank you very much for your help in advance? Marion 2012/4/12 Ista Zahn > Hi Marion, > > Did you look at the help file? Did you try use.value.labels = FALSE? > > Best, > Ista > > On Thu, Apr 12, 2012 at 6:28 AM, Marion Wenty > wrote: > > Dear people, > > > > 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. > > > > Thank you very much in advance for your help! > > > > Marion > > > >[[alternative HTML version deleted]] > > > > __ > > 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. > [[alternative HTML version deleted]] __ 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.
Re: [R] Reading SPSS: underlying numerical codes
Hi Marion, Did you look at the help file? Did you try use.value.labels = FALSE? Best, Ista On Thu, Apr 12, 2012 at 6:28 AM, Marion Wenty wrote: > Dear people, > > 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. > > Thank you very much in advance for your help! > > Marion > > [[alternative HTML version deleted]] > > __ > 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. __ 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.