| To: | Luis Apiolaza <luis.apiolaza@forestrytas.com.au>, s-news@lists.biostat.wustl.edu |
|---|---|
| Subject: | Re: Using apply for functions with more |
| From: | "Richard M. Heiberger" <rmh@temple.edu> |
| Date: | Tue, 7 Jun 2005 08:26:54 -0400 |
Yes, it is easy by using the ... arguments.
Here are two ways of approaching your example.
> tmp <- cbind(h=10:13, d=5:6)
> tmp
h d
[1,] 10 5
[2,] 11 6
[3,] 12 5
[4,] 13 6
> apply(tmp, 1, function(x) {x["h"] * x["d"]^2 * pi/4})
[1] 196.34954 311.01767 235.61945 367.56634
> sapply(1:nrow(tmp), function(i, h, d) {h[i] * d[i]^2 * pi/4}, h=tmp[,"h"],
> d=tmp[,"d"])
[1] 196.34954 311.01767 235.61945 367.56634
>
The first uses the apply() function on the matrix and no extra arguments.
The second uses the sapply() function on the vector of indices and
uses that to index into the extra arguments.
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Using apply for functions with more than one argument, Luis Apiolaza |
|---|---|
| Next by Date: | Re: Using apply for functions with more than one argument, Sarah Goslee |
| Previous by Thread: | Using apply for functions with more than one argument, Luis Apiolaza |
| Next by Thread: | Re: Comparing volatility, Samir Saadi |
| Indexes: | [Date] [Thread] [Top] [All Lists] |