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? 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 at snu.ac.kr Mobile: +82-10-6877-2109 Office : +82-2-880-9142 Fax: +82-2-880-8389
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 at snu.ac.kr > Mobile: +82-10-6877-2109 > Office : +82-2-880-9142 > Fax: +82-2-880-8389Haris Skiadas Department of Mathematics and Computer Science Hanover College
For one thing, get rid of the for() loop. The following may or may not be faster, and may or may not use more memory (an issue if x1 and x2 are long), but it's easier to read. n <- length(x1) y <- numeric(n) tmp <- x1>=5 & x1 <= 10 & x2 >= 5 & x2 <= 10 y[tmp] <- 0.631*x1[tmp]^0.55 * x2[tmp]^0.65 # and similarly for the other three conditions I would put in a test at the beginning to make sure that x1 and x2 are the same length: if (length(x1) != length(x2)) stop("Problem: x1 and x2 have different lengths") -Don At 2:37 PM +0900 4/16/07, 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? > >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 at snu.ac.kr >Mobile: +82-10-6877-2109 >Office : +82-2-880-9142 >Fax: +82-2-880-8389 > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA