[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Jin . Li
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

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Pascal Oettli
Hello, Try: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Regards Le 12/07/03 16:18, jin...@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

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Petr PIKAL
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 to

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Jim Lemon
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

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread David Winsemius
On Jul 3, 2012, at 5:08 AM, Jim Lemon wrote: 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

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Bart Joosen
And one more alternative: a1$h2 - apply(a1,1, function(x) if (x[h1]==H) 1 else 0 ) -- View this message in context: http://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.

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Jin . Li
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] - 1 Regards, Jin Geoscience Australia Disclaimer: This e-mail (and

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Peter Ehlers
On 2012-07-03 17:23, jin...@ga.gov.au wrote: 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] - 1 Interesting. My

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Pascal Oettli
Le 04/07/2012 12:43, Peter Ehlers a écrit : On 2012-07-03 17:23, jin...@ga.gov.au wrote: 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 -

Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]

2012-07-03 Thread Jin . Li
Thanks for your validation. Yes Peter's solution is the fastest, faster than the previous one by saving 25% time. It was missed out in my previous testing. Jin -Original Message- From: Pascal Oettli [mailto:kri...@ymail.com] Sent: Wednesday, 4 July 2012 2:07 PM To: Li Jin Cc: