Re: [R] sum of variables in function

2011-03-10 Thread Dennis Murphy
Hi: Assuming that z constitutes the last two elements of your input vector, try2 <- function(x) { n <- length(x) y <- x[1:(n - 2)] z <- x[n - 1] + x[n] sum(y) * z } x0 <- 1:4# result should be 21 x1 <- 1:8# z = 15, result = 21 * 15 = 315 try2(x0) try2(x1) HTH, Dennis On

Re: [R] sum of variables in function

2011-03-10 Thread Jonathan P Daily
How about: x <- rnorm(1000) fn <- function(k) sum(x[1:k]*(x[k+1] + x[k+2])) vals <- sapply(1:100, fn) vals -- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 "Is the room still a room when it