Have you considered using a different data structure: > # change the data structure > x <- data.frame( + type=rep(c('x1', 'x2', 'x3'), each=100), + high=c(d[,4], d[,5], d[,6]), + value=c(d[,1], d[,2], d[,3])) > head(x) type high value 1 x1 0 0.8936737 2 x1 0 -1.0472981 3 x1 1 1.9713374 4 x1 0 -0.3836321 5 x1 1 1.6541453 6 x1 1 1.5122127 > sapply(split(x$value, list(x$high, x$type)), function(a) c(min=min(a), > max=max(a))) 0.x1 1.x1 0.x2 1.x2 0.x3 1.x3 min -2.592328 1.364435 0.001314657 0.9447914 -2.9324797 0.9427577 max 1.324004 1.971337 0.925323476 0.9906600 0.8767704 1.5358077 > > >
On Thu, Feb 4, 2010 at 4:40 PM, David Freedman <3.14da...@gmail.com> wrote: > > 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ 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.