# [R] Elementary sapply question

Ajay Shah ajayshah at mayin.org
Mon Jun 21 20:24:24 CEST 2004

```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)
for (i in 1:3) {                |
}                               |

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)
for (i in 1:3) {
}

?

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

```