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 nfu...@gmail.com 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

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. Rrequire(microbenchmark) Rmicrobenchmark( +

[R] recode: how to avoid nested ifelse

2013-06-07 Thread Paul Johnson
In our Summer Stats Institute, I was asked a question that amounts to reversing the effect of the contrasts function (reconstruct an ordinal predictor from a set of binary columns). The best I could think of was to link together several ifelse functions, and I don't think I want to do this if the

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 -

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 data

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(x1,