On 8/02/2010 2:08 PM, David Winsemius wrote:
On Feb 7, 2010, at 8:26 PM, Nathan S. Watson-Haigh wrote:

I have a 2 column data.frame:

d[1:5,]
  a b
1    80015     C
2    80016     B
3    80023     C
4    80062     B
5    80069     B

I want to apply a function across each row:

for(i in 1:nrow(d)) {
+    myFun(con, d[i,]$a, d[i,]$b)
+ }

How do I do this using apply()? I'm unsure how to tell apply() to
pass data from columns a and b for a given row as arguments to the
function myFun().
apply(d, 1, function(x) myFun(x[1], x[2]) )

The reason you cannot use the "$" operator is that the row is passed
to the function as a vector, rather than as a list.


Thanks for the info, it was ever so slightly out......But for anyone else who might read this, the full working code of my example was as follows:

apply(d, 1, function(x) myFun(con, x[1], x[2]))

Cheers,
Nathan

--------------------------------------------------------
Dr. Nathan S. Watson-Haigh
OCE Post Doctoral Fellow
CSIRO Livestock Industries
University Drive
Townsville, QLD 4810
Australia

Tel: +61 (0)7 4753 8548
Fax: +61 (0)7 4753 8600
Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html

______________________________________________
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