I am discovering sapply! :-) Could you please help me with a very elementary question? Here is what I know. The following two programs generate the same answer. --------------------------------+---------------------------------------- Loops version | sapply version --------------------------------+---------------------------------------- | f <- function(x) { | f <- function(x) { return(x*x) | return(x*x) } | } values = c(2,4,8) | values = c(2,4,8) answers=numeric(3) | answers = sapply(values, f) for (i in 1:3) { | answers[i] = f(values[i]) | } | and this is cool! My problem is this. Suppose I have: pythagorean <- function(x, y) { return(x*x + y*y) } then how do I utilise sapply to replace fixed.x = 3 y.values = c(3,4,5) answers=numeric(3) for (i in 1:3) { answers[i] = pythagorean(fixed.x, y.values[i]) } ? I have read the sapply docs, and don't know how to tell him that the list values that he'll iterate over "fit in" as y.values[i]. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
At least two ways: 1. Use extra argument in the function being sapply()'ed; e.g.,> f <- function(x, y) x*x + y*y > x <- 3:5 > sapply(x, f, 3)[1] 18 25 34 [See the "..." argument in ?sapply.] 2. More generally, if both x and y are vectors (of the same length), then you can use mapply(); e.g.,> x <- 1:3 > y <- 3:5 > pyth <- function(x, y) x*x + y*y > mapply(pyth, x, y)[1] 10 20 34 HTH, Andy> From: Ajay Shah > > I am discovering sapply! :-) Could you please help me with a very > elementary question? > > Here is what I know. The following two programs generate the > same answer. > > --------------------------------+----------------------------- > ----------- > Loops version | sapply version > --------------------------------+----------------------------- > ----------- > | > f <- function(x) { | f <- function(x) { > return(x*x) | return(x*x) > } | } > values = c(2,4,8) | values = c(2,4,8) > answers=numeric(3) | answers = sapply(values, f) > for (i in 1:3) { | > answers[i] = f(values[i]) | > } | > > and this is cool! > > My problem is this. Suppose I have: > pythagorean <- function(x, y) { > return(x*x + y*y) > } > > then how do I utilise sapply to replace > fixed.x = 3 > y.values = c(3,4,5) > answers=numeric(3) > for (i in 1:3) { > answers[i] = pythagorean(fixed.x, y.values[i]) > } > > ? > > I have read the sapply docs, and don't know how to tell him that the > list values that he'll iterate over "fit in" as y.values[i]. > > -- > Ajay Shah Consultant > ajayshah at mayin.org Department of Economic Affairs > http://www.mayin.org/ajayshah Ministry of Finance, New Delhi > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
If you have something like lapply(x, f, ...) What lapply() does (which is the same as what sapply() does, except sapply() tries to `simplify' the result) is roughly: result <- vector(mode="list", length=length(x)) for (i in seq(along(x)) { result[[i]] <- f(x[i], ...) } I.e., it takes the first argument of lapply() and assume the function supplied as the second argument is to be applied to the components of the first argument one by one. Another thing you seem not to realize or remember is that in R, arguments to functions are matched by positions, if not named. Compare the output of: lapply(5, function(x, y) x*x + y*y, 1:3) lapply(1:3, function(x, y) x*x + y*y, 5) and change lapply() to sapply() and you'll probably see the light... HTH, Andy> From: Ajay Shah [mailto:ajayshah at mayin.org] > > I had asked: > > > My problem is this. Suppose I have: > > > pythagorean <- function(x, y) { > > > return(x*x + y*y) > > > } > > > > > > then how do I utilise sapply to replace > > > fixed.x = 3 > > > y.values = c(3,4,5) > > > answers=numeric(3) > > > for (i in 1:3) { > > > answers[i] = pythagorean(fixed.x, y.values[i]) > > > } > > On Mon, Jun 21, 2004 at 02:49:48PM -0400, Liaw, Andy wrote: > > At least two ways: > > > > 1. Use extra argument in the function being sapply()'ed; e.g., > > > > > f <- function(x, y) x*x + y*y > > > x <- 3:5 > > > sapply(x, f, 3) > > [1] 18 25 34 > > > > [See the "..." argument in ?sapply.] > > I am aware of the "..." in sapply(). I am unable to understand how > sapply will know where to utilise the x[i] values: as the 1st arg or > the 2nd arg for f(x, y)? > > That is, when I say: > > sapply(x, f, 3) > > how does sapply know that I mean: > > for (i in 3:5) { > f(i, 3) > } > > and not > > for (i in 3:5) { > f(3, i) > } > > How would we force sapply to use one or the other interpretation? > > Thanks, > > -ans. > > -- > Ajay Shah Consultant > ajayshah at mayin.org Department of Economic Affairs > http://www.mayin.org/ajayshah Ministry of Finance, New Delhi > >