Jin.Li at ga.gov.au
2012-Jul-03 07:18 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
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? Thanks in advance, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. ------------------------------------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]
Pascal Oettli
2012-Jul-03 07:27 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Hello, Try: > a1$h2 <- 0 > a1$h2[a1$h1=="H"] <- 1 Regards Le 12/07/03 16:18, Jin.Li at ga.gov.au a ?crit :> 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? > > Thanks in advance, > Jin > > Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. > ------------------------------------------------------------------------------------------------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Petr PIKAL
2012-Jul-03 08:27 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Hi> 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 toachieve> the desired result?Untested a1$h2 <- (a1$h1=="H")*1 Regards Petr> > Thanks in advance, > Jin > > Geoscience Australia Disclaimer: This e-mail (and files transmitted with> it) is intended only for the person or entity to which it is addressed.If> you are not the intended recipient, then you have received this e-mailby> mistake and any use, dissemination, forwarding, printing or copying of > this e-mail and its file attachments is prohibited. The security ofemails> transmitted cannot be guaranteed; by forwarding or replying to thisemail,> you acknowledge and accept these risks. >-------------------------------------------------------------------------------------------------------------------------> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Jim Lemon
2012-Jul-03 09:08 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
On 07/03/2012 05:18 PM, Jin.Li at 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
Bart Joosen
2012-Jul-03 11:13 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
And one more alternative: a1$h2 <- apply(a1,1, function(x) if (x["h1"]=="H") 1 else 0 ) -- View this message in context: r.789695.n4.nabble.com/Is-it-possible-to-remove-this-loop-SEC-UNCLASSIFIED-tp4635250p4635271.html Sent from the R help mailing list archive at Nabble.com.
Jin.Li at ga.gov.au
2012-Jul-04 00:23 UTC
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Thank you all for providing various alternatives. They are all pretty fast. Great help! Based on a test of a dataset with 800,000 rows, the time used varies from 0.04 to 11.56 s. The champion is:> a1$h2 <- 0 > a1$h2[a1$h1=="H"] <- 1Regards, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks.