Re: [R] Simplify simple code On Apr 16, 2007, at 1:37 AM, Dong-hyun Oh wrote: > Dear expeRts, > > I would simplify following code. > --------------------------------------------- > youtput <- function(x1, x2){ >    n <- length(x1) >    y <- vector(mode="numeric", length=n) >    for(i in 1:n){ >      if(x1[i] >=5 & x1[i] <= 10 & x2[i] >=5 & x2[i] <=10) >        y[i] <- 0.631 * x1[i]^0.55 * x2[i]^0.65 >      if(x1[i] >=10 & x1[i] <= 15 & x2[i] >=5 & x2[i] <=10) >        y[i] <- 0.794 * x1[i]^0.45 * x2[i]^0.65 >      if(x1[i] >=5 & x1[i] <= 10 & x2[i] >=10 & x2[i] <=15) >        y[i] <- 1.259 * x1[i]^0.55 * x2[i]^0.35 >      if(x1[i] >=10 & x1[i] <= 15 & x2[i] >=10 & x2[i] <=15) >        y[i] <- 1.585 * x1[i]^0.45 * x2[i]^0.35 >    } >    y > } > ---------------------------------------------- > Anyone can help me? > I hope someone comes up with something better, but here is one way: youtput <- function(x1, x2) { 	co1 <- matrix(c(0.631,0.794,1.259,1.585), c(2,2)) 	co2 <- c(0.55,0.45) 	co3 <- c(0.65,0.35) 	p1 <- findInterval(x1,c(5,10,15)) 	p2 <- findInterval(x2,c(5,10,15)) 	return( diag(co1[p1,p2]) * x1^co2[p1] * x2^co3[p2] ) } It is not clear at all what you wanted to happen when x1 and/or x2 is   not between 5 and 15, so I did not deal with those case. The above   command will choke in that case, and should be modified accordingly   depending on what you want. > Sincerely, > > =========================================== > Dong H. Oh > > Ph. D Candidate > Techno-Economics and Policy Program > College of Engineering, Seoul National University, > Seoul, 151-050, Republic of Korea > > E-mail:doriaba2@snu.ac.kr > Mobile: +82-10-6877-2109 > Office : +82-2-880-9142 > Fax: +82-2-880-8389 Haris Skiadas Department of Mathematics and Computer Science Hanover College ______________________________________________ R-help@stat.math.ethz.ch 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.