[R] If else
Val
valkremk at gmail.com
Sat Oct 31 16:23:05 CET 2015
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> wrote:
> In line.
>
> John Kane
> Kingston ON Canada
>
>
> > -----Original Message-----
> > From: valkremk at gmail.com
> > Sent: Fri, 30 Oct 2015 20:40:03 -0500
> > To: 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> wrote:
> >
> >> Using numeric for missing sounds like asking for trouble. But if you
> >> must, something like
> >>
> >> mydata$confusingWillCauseProblemsLater <-
> >> ifelse(
> >> 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> 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 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 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]]
More information about the R-help
mailing list