[R] Creating dummy variables

Jannis bt_jannis at yahoo.de
Thu Jun 3 16:40:30 CEST 2010


Was that the original code that you ran? As there appear to be several mistakes in the code:

1. In the gipsisoil stuff, there is a ')' too much
2. In the gambisoil stuff both > signs point in the same direction, you probably want one > and one <


My general suggestion would be to skip the loops altogether and vectorize your code:

datos$cambi=datos$codsuelo
datos$cambi[datos$codsuelo>=3.1 && datos$codsuelo <=3.3] <- 1

Another source of your error could be that datos$codtipo is not numeric. What does class(datos$codzuelo) say?


HTH
Jannis
> for (i in
> 1:length(datos$cambi)){if(datos$codsuelo[i]>=3.1
> &&
> datos$codsuelo[i]>=3.3){datos$cambi[i]=1}else{0} 
> }

--- Arantzazu Blanco Bernardeau <aramucia at hotmail.com> schrieb am Do, 3.6.2010:

> Von: Arantzazu Blanco Bernardeau <aramucia at hotmail.com>
> Betreff: [R] Creating dummy variables
> An: r-help at r-project.org
> Datum: Donnerstag, 3. Juni, 2010 14:11 Uhr
> 
> Hello R project
> I am a R beginner trying to create a dummy variable to
> clasificate soil types.
> So, I have a column in my database called codtipo (typecode
> in english) where soil type is coded as 
> 1.1 to 1.4 arenosol (I have 4 types)
> 2.1 to 2.3 calcisols 
> 4.1 to 4.4 fluvisols
> and so on
> To make dummy variables I understand that, I create
> different columns as for gipsisols
> datos$gipsi=datos$codsuelo
> for (i in
> 1:length(datos$gipsi)){if(datos$codsuelo[i]>=5.1
> &&
> (datos$codsuelo[i]<=5.4){datos$gipsi[i]=1}else{0}
> }
> for cambisols it should be
> datos$cambi=datos$codsuelo
> for (i in
> 1:length(datos$cambi)){if(datos$codsuelo[i]>=3.1
> &&
> datos$codsuelo[i]>=3.3){datos$cambi[i]=1}else{0} 
> }
> and so on... 
> but anyway R answers that a necesary value TRUE/FALSE is
> not existing.
> What can I do?
> thanks a lot!!
> 
> 
> Arantzazu Blanco Bernardeau
> Dpto de Química Agrícola, Geología y Edafología  
> Universidad de Murcia-Campus de Espinardo
> 
> 
> 
> 
> 
> 
> 
> > Date: Thu, 3 Jun 2010 06:51:42 -0700
> > From: lamprianou at yahoo.com
> > To: jorismeys at gmail.com
> > CC: r-help at r-project.org
> > Subject: Re: [R] ordinal variables
> > 
> > Thank you Joris,
> > I'll have a look into the commands you sent me. They
> look convincing. I hope my students will also see them in a
> positive way (although I can force them to pretend that they
> have a positive attitude)!
> > 
> > Dr. Iasonas Lamprianou
> > 
> > 
> > 
> > 
> > 
> > Assistant Professor (Educational Research and
> Evaluation)
> > 
> > Department of Education Sciences
> > 
> > European University-Cyprus
> > 
> > P.O. Box 22006
> > 
> > 1516 Nicosia
> > 
> > Cyprus 
> > 
> > Tel.: +357-22-713178
> > 
> > Fax: +357-22-590539
> > 
> > 
> > 
> > 
> > 
> > Honorary Research Fellow
> > 
> > Department of Education
> > 
> > The University of Manchester
> > 
> > Oxford Road, Manchester M13 9PL, UK
> > 
> > Tel. 0044  161 275 3485
> > 
> > iasonas.lamprianou at manchester.ac.uk
> > 
> > --- On Thu, 3/6/10, Joris Meys <jorismeys at gmail.com>
> wrote:
> > 
> > From: Joris Meys <jorismeys at gmail.com>
> > Subject: Re: [R] ordinal variables
> > To: "Iasonas Lamprianou" <lamprianou at yahoo.com>
> > Cc: r-help at r-project.org
> > Date: Thursday, 3 June, 2010, 14:35
> > 
> > see ?factor and ?as.factor. On ordered factors you can
> technically do a spearman without problem, apart from the
> fact that a spearman test by definition cannot give exact
> p-values with ties present.
> > 
> > x <- sample(c("a","b","c","d","e"),100,replace=T)
> > 
> > y <- sample(c("a","b","c","d","e"),100,replace=T)
> > 
> > x.ordered <-
> factor(x,levels=c("e","b","a","d","c"),ordered=T)
> > 
> > x.ordered
> > y.ordered <-
> factor(y,levels=c("e","b","a","d","c"),ordered=T)
> > y.ordered
> > 
> > cor.test(x.ordered,y.ordered,method="spearman")
> > 
> > require(pspearman)
> > 
> > spearman.test(x.ordered,y.ordered)
> > 
> > R commander has some menu options to deal with
> factors. R commander also provides a scripting window.
> Please do your students a favor, and show them how to use
> those commands. 
> > 
> > 
> > Cheers
> > Joris
> > 
> > 
> > On Thu, Jun 3, 2010 at 2:25 PM, Iasonas Lamprianou
> <lamprianou at yahoo.com>
> wrote:
> > 
> > Dear colleagues,
> > 
> > 
> > 
> > I teach statistics using SPSS. I want to use R
> instead. I hit on one problem and I need some quick advice.
> When I want to work with ordinal variables, in SPSS I can
> compute the median or create a barchart or compute a
> spearman correlation with no problems. In R, if I "read" the
> ordinal variable as numeric, then I cannot do a barplot
> because I miss the category names. If I read the variables
> as characters, then I cannot run a spearman. How can I read
> a variable as numeric, still have the chance to assign value
> labels, and be able to get table of frequencies etc? I want
> to be able to do all these things in R commander. My
> students will probable be scared away if I try anything else
> other than R commander (just writing commands will not make
> them happy).
> > 
> > 
> > 
> > 
> > I hope I am not asking for too much. Hopefully there
> is a way
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > ______________________________________________
> > 
> > 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.
> > 
> > 
> > 
> > 
> > -- 
> > Joris Meys
> > Statistical Consultant
> > 
> > Ghent University
> > Faculty of Bioscience Engineering 
> > Department of Applied mathematics, biometrics and
> process control
> > 
> > 
> > Coupure Links 653
> > B-9000 Gent
> > 
> > tel : +32 9 264 59 87
> > Joris.Meys at Ugent.be
> 
> > -------------------------------
> > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
> > 
> > 
> > 
> > 
> > 
> >       
> >     [[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.
>     
>         
>           
>   
> _________________________________________________________________
> Citas sin compromiso por Internet Te damos las claves para
> encontrar pareja en la red
> 
>     [[alternative HTML version deleted]]
> 
> 
> -----Integrierter Anhang folgt-----
> 
> ______________________________________________
> 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