On 8/02/2010 4:33 PM, Jim Lemon wrote:
On 02/08/2010 12: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().

Hi Nathan,
apply doesn't work with data frames unless they can be coerced to
matrices or arrays (and sometimes not even then). What's wrong with
using the code you have above?

Jim


Nothing, I seem to remember seeing somewhere that R is slow at doing for loops and it's best to use apply. But I suppose that depends more on whether myFun() is the bottleneck or not!

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