Hi,

I'm a newbie when it comes to R, and I'm trying to figure out how to use
vectorization as opposed to for loops.  In particular, how can I create a
function that is applied on each element of a row, but can access previous
elements relative to that element?

My problem: I want to calculate something like x[i] = x[i] / x[i - 1] for
each element of a vector x:

ex.

x <- data.frame(a = c(1:10))

# incorrect
foo = function(y) {

y[n] <- y[n] / y[n-1]

}

div <- lapply(x, foo)

Any help would be greatly appreciated.

Regards,
-Amol Shelat

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to