Just commenting on the email subject, not the content (which you have already been helped with): there is no need to *ever* create a dummy variable for regression in R if what you mean by this is what is conventionally meant. R will create the model matrix with appropriate "dummy variables" for factors as needed. See ?contrasts and ?C for relevant details and/or consult an appropriate R tutorial.
Of course, if this is not what you meant, than ignore. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Aug 5, 2016 at 1:49 PM, <ruipbarra...@sapo.pt> wrote: > Hello, > > Your ifelse will never work because > reasons$salutation== "Mr" & reasons$salutation=="Father" is always FALSE > and so is reasons$salutation=="Mrs" & reasons$salutation=="Miss". > Try instead | (or), not & (and). > > Hope this helps, > > Rui Barradas > > > > Citando Shivi Bhatia <shivipm...@gmail.com>: > >> Dear Team, >> >> I need help with the below code in R: >> >> gender_rec<- c('Dr','Father','Mr'=1, 'Miss','MS','Mrs'=2, 3) >> >> reasons$salutation<- gender_rec[reasons$salutation]. >> >> This code gives me the correct output but it overwrites the >> reason$salutation variable. I need to create a new variable gender to >> capture gender details and leave salutation as it is. >> >> i tried the below syntax but it is converting all to 1. >> >> reasons$gender<- ifelse(reasons$salutation== "Mr" & reasons$salutation== >> "Father","Male", ifelse(reasons$salutation=="Mrs" & reasons$salutation== >> "Miss","Female",1)) >> >> Please suggest. >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.htmland provide commented, >> minimal, self-contained, reproducible code. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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 -- To UNSUBSCRIBE and more, see 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.