Re: [R] recode: how to avoid nested ifelse

2013-06-10 Thread Paul Johnson
Thanks, guys. On Sat, Jun 8, 2013 at 2:17 PM, Neal Fultz wrote: > rowSums and Reduce will have the same problems with bad data you alluded > to earlier, eg > cg = 1, hs = 0 > > But that's something to check for with crosstabs anyway. > > This "wrong data" thing is a distraction here. I guess I

Re: [R] recode: how to avoid nested ifelse

2013-06-08 Thread Neal Fultz
rowSums and Reduce will have the same problems with bad data you alluded to earlier, eg cg = 1, hs = 0 But that's something to check for with crosstabs anyway. Side note: you should check out the microbenchmark pkg, it's quite handy. R>require(microbenchmark) R>microbenchmark( + f1(cg,hs,es

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Joshua Wiley
I still argue for na.rm=FALSE, but that is cute, also substantially faster f1 <- function(x1, x2, x3) do.call(paste0, list(x1, x2, x3)) f2 <- function(x1, x2, x3) pmax(3*x3, 2*x2, es, 0, na.rm=FALSE) f3 <- function(x1, x2, x3) Reduce(`+`, list(x1, x2, x3)) f4 <- function(x1, x2, x3) rowSums(cbind(

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Neal Fultz
I would do this to get the highest non-missing level: x <- pmax(3*cg, 2*hs, es, 0, na.rm=TRUE) rock chalk... -nfultz On Fri, Jun 07, 2013 at 06:24:50PM -0700, Joshua Wiley wrote: > Hi Paul, > > Unless you have truly offended the data generating oracle*, the > pattern: NA, 1, NA, should be a da

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Joshua Wiley
Hi Paul, Unless you have truly offended the data generating oracle*, the pattern: NA, 1, NA, should be a data entry error --- graduating HS implies graduating ES, no? I would argue fringe cases like that should be corrected in the data, not through coding work arounds. Then you can just do: x <-