Mike Day
2005-Jun-13 07:22 UTC
[R] assignment of inidividual variables from spss save files
New to R, can't afford SPSS! Why can't I assign spss variables? Here are the details. > ### I've read in my collaborator's sav file using read.spss, eg > children = read.spss(filename) > ### It has many variables > length(children) [1] 347 > ### and I would like to assign individual variables. > ### I can of course type out all the ones I want, eg > ADULTS11<-children[18] > ### But I'm lazy, so tried: > (function (x,n){assign(names(x[n]),x[n])})(children,2) > ### where > names(children[2]) [1] "ADNUTS06" > ### but > ADNUTS06 Error: Object "ADNUTS06" not found > ### I've tried lots of variations, including creating and > ### evaluating statements such as "ADNUTS06<-children[2]", > ### but without apparently yielding the required variable. I expect the answer will be "You shouldn?t start from here!" Ideas welcome. Thanks, Mike
Peter Dalgaard
2005-Jun-13 07:39 UTC
[R] assignment of inidividual variables from spss save files
Mike Day <mike_liz.day at tiscali.co.uk> writes:> New to R, can't afford SPSS! > > Why can't I assign spss variables? Here are the details. > > > ### I've read in my collaborator's sav file using read.spss, eg > > children = read.spss(filename) > > ### It has many variables > > length(children) > [1] 347 > > ### and I would like to assign individual variables. > > ### I can of course type out all the ones I want, eg > > ADULTS11<-children[18] > > ### But I'm lazy, so tried: > > (function (x,n){assign(names(x[n]),x[n])})(children,2) > > ### where > > names(children[2]) > [1] "ADNUTS06" > > ### but > > ADNUTS06 > Error: Object "ADNUTS06" not found > > ### I've tried lots of variations, including creating and > > ### evaluating statements such as "ADNUTS06<-children[2]", > > ### but without apparently yielding the required variable. > > I expect the answer will be "You shouldn?t start from here!" > Ideas welcome.Right. Instead, read up on the attach() function and with(). Read through "An Introduction to R" (manual, comes with R) and pay particular attention to chapter 6(.3). Beware the tricky bits relating to the search path and masking of variables. -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Romain Francois
2005-Jun-13 07:47 UTC
[R] assignment of inidividual variables from spss save files
Le 13.06.2005 09:22, Mike Day a 侀crit :>New to R, can't afford SPSS! > >Why can't I assign spss variables? Here are the details. > > > ### I've read in my collaborator's sav file using read.spss, eg > > children = read.spss(filename) > > ### It has many variables > > length(children) >[1] 347 > > ### and I would like to assign individual variables. > > ### I can of course type out all the ones I want, eg > > ADULTS11<-children[18] > > ### But I'm lazy, so tried: > > (function (x,n){assign(names(x[n]),x[n])})(children,2) > > ### where > > names(children[2]) >[1] "ADNUTS06" > > ### but > > ADNUTS06 >Error: Object "ADNUTS06" not found > > ### I've tried lots of variations, including creating and > > ### evaluating statements such as "ADNUTS06<-children[2]", > > ### but without apparently yielding the required variable. > >I expect the answer will be "You shouldn抰 start from here!" >Ideas welcome. > >Thanks, Mike > >?attach may be what you are looking for -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
BXC (Bendix Carstensen)
2005-Jun-13 10:43 UTC
[R] assignment of inidividual variables from spss save files
The key to solving your problem is that read.spss per default gives you a *list* and not a *dataframe* (can anyone explain this choice of default?). So most likely wou want: children = read.spss(filename,to.data.frame=TRUE) attach(children) or to get things a little more handy: children <- read.spss(filename,to.data.frame=TRUE) names(children) <- tolower( names( children ) ) attach(children) best, Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: Romain Francois [mailto:francoisromain at free.fr] > Sent: Monday, June 13, 2005 9:48 AM > To: Mike Day; RHELP > Subject: Re: [R] assignment of inidividual variables from > spss save files > > > Le 13.06.2005 09:22, Mike Day a ??crit : > > >New to R, can't afford SPSS! > > > >Why can't I assign spss variables? Here are the details. > > > > > ### I've read in my collaborator's sav file using read.spss, eg > > > children = read.spss(filename) ### It has many variables > > > length(children) > >[1] 347 > > > ### and I would like to assign individual variables. > > > ### I can of course type out all the ones I want, eg > > > ADULTS11<-children[18] ### But I'm lazy, so tried: > > > (function (x,n){assign(names(x[n]),x[n])})(children,2) > > > ### where > > > names(children[2]) > >[1] "ADNUTS06" > > > ### but > > > ADNUTS06 > >Error: Object "ADNUTS06" not found > > > ### I've tried lots of variations, including creating and ### > > > evaluating statements such as "ADNUTS06<-children[2]", ### but > > > without apparently yielding the required variable. > > > >I expect the answer will be "You shouldn't start from here!" Ideas > >welcome. > > > >Thanks, Mike > > > > > ?attach may be what you are looking for > > > -- > visit the R Graph Gallery : http://addictedtor.free.fr/graphiques > ~~~~~~~~ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ > ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr > ~~~~~~ > ~~~~ Etudiant ISUP - CS3 - Industrie et Services > ~~~~ > ~~ http://www.isup.cicrp.jussieu.fr/ > ~~ > ~~~~ Stagiaire INRIA Futurs - Equipe SELECT > ~~~~ > ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html > ~~~~~~ > ~~~~~~~~ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ > > >