Dear helpers. I often need to make dichotomous variables out of continuous ones (yes, I realize the problems with throwing away much of the information), but I then like to check the min and max of each category. I have the following simple code to do this that cuts each variable (x1,x2,x3) at the 90th percentile, and then prints the min and max of each category:
d=data.frame(x1=rnorm(100),x2=runif(100)); d=transform(d,x3=x1-x2) d[,4:6]=data.frame(sapply(d,function(v)as.numeric(v>=quantile(v,0.9)))); names(d)[4:6]=c('x1high','x2high','x3high') head(d) for (i in 1:3){print(do.call(rbind,by(d[,i],d[,i+3],function(x)(c(min(x),max(x))))))} Is there a way to replace the ugly for loop in the last line with some type of apply function that would know that my continuous and indicator variable are 3 variables apart in the dataframe? Thanks very much David Freedman -- View this message in context: http://n4.nabble.com/replace-a-for-loop-with-lapply-or-relative-tp1469453p1469453.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.