On 07/03/2012 05:18 PM, jin...@ga.gov.au wrote:
Hi all,
I would like create a new column in a data.frame (a1) to store 0, 1 data
converted from a factor as below.
a1$h2<-NULL
for (i in 1:dim(a1)[1]) {
if (a1$h1[i]=="H") a1$h2[i]<-1 else a1$h2[i]<-0
}
My question: is it possible to remove the loop from above code to achieve the
desired result?
Hi Jin,
Just to provide you with an embarrassment of alternatives:
a1$h2<-ifelse(a1$h1=="H",1,0)
Jim
______________________________________________
R-help@r-project.org mailing list
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.