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 testing shows that Petr's solution is about
twice as fast. Not that it matters much - the time is pretty
small in any case.
a0 <- data.frame(h1 = sample(c("H","J","K"), 1e7, replace = TRUE),
stringsAsFactors = FALSE)
a1 <- a0
system.time({a1$h2 <- 0; a1$h2[a1$h1 == "H"] <- 1})
# user system elapsed
# 1.47 0.48 1.96
a11 <- a1
a1 <- a0
system.time(a1$h2 <- (a1$h1 == "H") * 1)
# user system elapsed
# 0.37 0.17 0.56
a12 <- a1
all.equal(a11,a12)
#[1] TRUE
Peter Ehlers
Regards,
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.
______________________________________________
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.