[R] If else

Val valkremk at gmail.com
Sun Nov 1 01:37:13 CET 2015


Thank you Professor John,

After I clicked the send button, I tried and the following also worked for
me.

mydata$sex1  <- ifelse(is.na(mydata$sex), 0, ifelse(mydata$sex == "M", 1,
2)))

Thank you  all again!






On Sat, Oct 31, 2015 at 7:24 PM, Fox, John <jfox at mcmaster.ca> wrote:

> Dear Val,
>
> > -----Original Message-----
> > From: Val [mailto:valkremk at gmail.com]
> > Sent: October 31, 2015 5:57 PM
> > To: Fox, John <jfox at mcmaster.ca>
> > Subject: Re: [R] If else
> >
> > Dear Professor John,
> >
> >
> > Thank you very much for your elegant answer.
> >
> > This one dump it on the screen. How can I send it to a a file?
>
> There are several ways to do this; one is
>
> mydata <- within(mydata, sex1 <- ifelse(is.na(sex), 0, ifelse(sex == "M",
> 1, 2)))
> write.table(mydata, "mydata.txt", quote=FALSE, row.names=FALSE)
>
> This assumes that you want a space-separated data file with variable names
> but not case names.
>
> A couple of more general points:
>
> (1) Since you originally asked the question on the r-help list, I've cc'd
> this response to the list.
>
> (2) If you're going to use R beyond this one application, you'll probably
> want to learn some more about it.
>
> Best,
>  John
>
> >
> >
> > within(mydata, sex1 <- ifelse(is.na <http://is.na> (sex), 0, ifelse(sex
> == "M", 1,
> > 2)))
> >   id  sex sex1
> > 1  1 <NA>    0
> > 2  2 <NA>    0
> > 3  3    M    1
> > 4  4    F    2
> > 5  5    M    1
> > 6  6    F    2
> > 7  7    F    2
> >
> >
> > Thank you in advance,
> >
> > Val
> >
> >
> >
> >
> > On Sat, Oct 31, 2015 at 1:44 PM, Fox, John <jfox at mcmaster.ca
> > <mailto:jfox at mcmaster.ca> > wrote:
> >
> >
> >       Dear Val,
> >
> >       I haven't been following this thread, but there are several ways
> to do
> > what you want, for example,
> >
> >       > within(mydata, sex1 <- ifelse(is.na <http://is.na> (sex), 0,
> ifelse(sex
> > == "M", 1, 2)))
> >         id  sex sex1
> >       1  1 <NA>    0
> >       2  2 <NA>    0
> >       3  3    M    1
> >       4  4    F    2
> >       5  5    M    1
> >       6  6    F    2
> >       7  7    F    2
> >
> >       My apologies if someone has already suggested that.
> >
> >       Best,
> >        John
> >
> >       -----------------------------------------------
> >       John Fox, Professor
> >       McMaster University
> >       Hamilton, Ontario, Canada
> >       http://socserv.socsci.mcmaster.ca/jfox/
> >
> >
> >
> >       > -----Original Message-----
> >       > From: R-help [mailto:r-help-bounces at r-project.org <mailto:
> r-help-
> > bounces at r-project.org> ] On Behalf Of Val
> >       > Sent: Saturday, October 31, 2015 1:16 PM
> >       > To: Jeff Newmiller
> >       > Cc: r-help at R-project.org (r-help at r-project.org <mailto:r-help at r-
> > project.org> )
> >       > Subject: Re: [R] If else
> >       >
> >
> >       > Hi Jeff,
> >       >
> >       > I thought I answered. Yes I was not clear about it.  The further
> >       > analysis
> >       > will no  be done by R.  It is another software that will not
> accept a
> >       > character response variable.
> >       >
> >       > Why R is so complicated to do that.  If it is SAS then I can do
> it on
> >       > one
> >       > statement. .
> >       >
> >       >
> >       > On Sat, Oct 31, 2015 at 11:39 AM, Jeff Newmiller
> >       > <jdnewmil at dcn.davis.ca.us <mailto:jdnewmil at dcn.davis.ca.us> >
> >       > wrote:
> >       >
> >       > > You haven't actually answered John's question as to the type of
> >       > analysis
> >       > > you plan to do. It still looks from here like you should be
> using
> >       > factor
> >       > > data rather than numeric, but since you are not being clear we
> > cannot
> >       > give
> >       > > specifics as to how to proceed.
> >       > >
> ----------------------------------------------------------------------
> >       > -----
> >       > > Jeff Newmiller                        The     .....
>  .....  Go
> >       > Live...
> >       > > DCN:<jdnewmil at dcn.davis.ca.us
> > <mailto:jdnewmil at dcn.davis.ca.us> >        Basics: ##.#.       ##.#.
> Live
> >       > > Go...
> >       > >                                       Live:   OO#.. Dead: OO#..
> >       > Playing
> >       > > Research Engineer (Solar/Batteries            O.O#.
>  #.O#.  with
> >       > > /Software/Embedded Controllers)               .OO#.       .OO#.
> >       > rocks...1k
> >       > >
> ----------------------------------------------------------------------
> >       > -----
> >       > > Sent from my phone. Please excuse my brevity.
> >       > >
> >       > > On October 31, 2015 8:23:05 AM PDT, Val <valkremk at gmail.com
> > <mailto:valkremk at gmail.com> > wrote:
> >       > > >Hi All,
> >       > > >
> >       > > >
> >       > > >Yes I need  to change to numeric  because I am preparing a
> data
> > set
> >       > > >for
> >       > > >further  analysis. The variable to be changed  from character
> to
> >       > > >numeric
> >       > > >(in this case, sex) will be a response variable.  Some records
> > have
> >       > > >missing
> >       > > >observation on sex and it is blank.
> >       > > >     id  sex
> >       > > >      1
> >       > > >      2
> >       > > >      3  M
> >       > > >      4  F
> >       > > >      5  M
> >       > > >      6  F
> >       > > >      7  F
> >       > > >
> >       > > >I am reading the data like this
> >       > > >
> >       > > >mydata <- read.csv(header=TRUE, text=', sep=", ")
> >       > > >     id  sex
> >       > > >      1   NA
> >       > > >      2  NA
> >       > > >      3  M
> >       > > >      4  F
> >       > > >      5  M
> >       > > >      6  F
> >       > > >      7  F
> >       > > >
> >       > > >The  data set is huge   (>250,000)
> >       > > >
> >       > > >
> >       > > >I want the output like this
> >       > > >
> >       > > >     id  sex    sex1
> >       > > >      1   NA    0
> >       > > >      2  NA     0
> >       > > >      3  M       1
> >       > > >      4  F       2
> >       > > >      5  M      1
> >       > > >      6  F       2
> >       > > >      7  F       2
> >       > > >
> >       > > >Thank you in advance
> >       > > >
> >       > > >
> >       > > >On Sat, Oct 31, 2015 at 5:59 AM, John Kane
> > <jrkrideau at inbox.com <mailto:jrkrideau at inbox.com> >
> >       > wrote:
> >       > > >
> >       > > >> In line.
> >       > > >>
> >       > > >> John Kane
> >       > > >> Kingston ON Canada
> >       > > >>
> >       > > >>
> >       > > >> > -----Original Message-----
> >       > > >> > From: valkremk at gmail.com <mailto:valkremk at gmail.com>
> >       > > >> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
> >       > > >> > To: istazahn at gmail.com <mailto:istazahn at gmail.com>
> >       > > >> > Subject: Re: [R] If else
> >       > > >> >
> >       > > >> > I am trying to change the mydata$sex  from character to
> > numeric
> >       > > >>
> >       > > >> Why?
> >       > > >>  As Ista (mydata$confusingWillCauseProblemsLater) has
> > pointed out
> >       > > >this is
> >       > > >> a very unusual thing to do in R.
> >       > > >>
> >       > > >> Is there a very specific reason for doing this in your
> analysis.
> >       > > >> Otherwise it may better to leave the coding as NA. Some of
> > the data
> >       > > >mungers
> >       > > >> here may be able to suggest which is the best strategy in R.
> >       > > >>
> >       > > >> R is 'weird' compared to more mundane stats packages such
> > as SAS or
> >       > > >SPSS
> >       > > >> and common techniques that one would use with them often
> > are not
> >       > > >> appropriate in R.
> >       > > >>
> >       > > >>
> >       > > >>
> >       > > >>
> >       > > >> > I want teh out put like
> >       > > >> >    id  sex
> >       > > >> >       1  NA   0
> >       > > >> >       2  NA   0
> >       > > >> >       3  M     1
> >       > > >> >       4  F     2
> >       > > >> >       5  M    1
> >       > > >> >       6  F     2
> >       > > >> >       7  F    2
> >       > > >> >
> >       > > >> > mydata$sex1 <- 0
> >       > > >> > if(mydata$sex =="M " ){
> >       > > >> >   mydata$sex1<-1
> >       > > >> > } else {
> >       > > >> >   mydata$sex1<-2
> >       > > >> > }
> >       > > >> >
> >       > > >> > mydata$sex1
> >       > > >> >
> >       > > >> > Warning message:In if (mydata$sex == "M ") { :
> >       > > >> >   the condition has length > 1 and only the first element
> will
> > be
> >       > > >> > used> mydata$sex1[1] 2 2 2 2 2 2 2 2
> >       > > >> >
> >       > > >> >>
> >       > > >> >
> >       > > >> >
> >       > > >> > On Fri, Oct 30, 2015 at 8:28 PM, Ista Zahn
> > <istazahn at gmail.com <mailto:istazahn at gmail.com> >
> >       > > >wrote:
> >       > > >> >
> >       > > >> >> Using numeric for missing sounds like asking for trouble.
> > But if
> >       > > >you
> >       > > >> >> must, something like
> >       > > >> >>
> >       > > >> >> mydata$confusingWillCauseProblemsLater <-
> >       > > >> >>   ifelse(
> >       > > >> >>     is.na <http://is.na> (mydata$sex),
> >       > > >> >>     0,
> >       > > >> >>     as.numeric(factor(mydata$sex,
> >       > > >> >>                       levels = c("M", "F"))))
> >       > > >> >>
> >       > > >> >> should do it.
> >       > > >> >>
> >       > > >> >> Best,
> >       > > >> >> Ista
> >       > > >> >>
> >       > > >> >> On Fri, Oct 30, 2015 at 9:15 PM, Val <valkremk at gmail.com
> > <mailto:valkremk at gmail.com> > wrote:
> >       > > >> >>> Hi all,
> >       > > >> >>> Iam trying to change character  to numeric but have
> > probelm
> >       > > >> >>>
> >       > > >> >>> mydata <- read.table(header=TRUE, text=', sep=" "
> >       > > >> >>>      id  sex
> >       > > >> >>>       1  NA
> >       > > >> >>>       2  NA
> >       > > >> >>>       3  M
> >       > > >> >>>       4  F
> >       > > >> >>>       5  M
> >       > > >> >>>       6  F
> >       > > >> >>>       7  F
> >       > > >> >>>        ')
> >       > > >> >>>
> >       > > >> >>> if sex is missing then sex=0;
> >       > > >> >>> if sex is"M" then sex=1;
> >       > > >> >>> if sex is"F" then sex=2;
> >       > > >> >>>
> >       > > >> >>> Any help please ?
> >       > > >> >>>
> >       > > >> >>>         [[alternative HTML version deleted]]
> >       > > >> >>>
> >       > > >> >>> ______________________________________________
> >       > > >> >>> R-help at r-project.org <mailto:R-help at 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.
> >       > > >> >>
> >       > > >> >
> >       > > >> >       [[alternative HTML version deleted]]
> >       > > >> >
> >       > > >> > ______________________________________________
> >       > > >> > R-help at r-project.org <mailto:R-help at 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.
> >       > > >>
> >       > > >>
> > ____________________________________________________________
> >       > > >> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins,
> > sharks &
> >       > orcas
> >       > > >on
> >       > > >> your desktop!
> >       > > >> Check it out at http://www.inbox.com/marineaquarium
> >       > > >>
> >       > > >>
> >       > > >>
> >       > > >
> >       > > >       [[alternative HTML version deleted]]
> >       > > >
> >       > > >______________________________________________
> >       > > >R-help at r-project.org <mailto:R-help at 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.
> >       > >
> >       > >
> >       >
> >       >       [[alternative HTML version deleted]]
> >       >
> >       > ______________________________________________
> >       > R-help at r-project.org <mailto:R-help at 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.
> >
> >
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list