[R] If else
Ista Zahn
istazahn at gmail.com
Sat Oct 31 02:28:19 CET 2015
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.
More information about the R-help
mailing list