[R] [FORGED] If else
Rolf Turner
r.turner at auckland.ac.nz
Sat Oct 31 03:40:55 CET 2015
On 31/10/15 14:15, Val 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 ?
sex <- c(NA,NA,"M","F","M","F","F")
# 1.
match(sex,c(NA,"M","F"))-1
# 2.
as.numeric(factor(sex,exclude=NULL,levels=c(NA,"M","F")))-1
cheers,
Rolf Turner
P. S. As others have told you, converting character to numeric is highly
ill-advised.
R. T.
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
More information about the R-help
mailing list