[R] applying a different function to rows of a dataframe

Anne York york at noaa.gov
Mon Dec 16 20:07:02 CET 2002


Here is a simple example of what I would like to do:

Given a data frame foo with variables x and fn. Suppose fn is a vector of
characters which correspond to  names of previously defined  functions
which have only one argument. I would like a vector returned where fn is
applied to x

foo <-  data.frame(x=c(2,5,7), fn = letters[c(6,7,6)])
foo$fn <- as.character(foo$fn)

 "f" <- function(x){17*x}
 "g" <- function(x){3*x}


foo
  x fn
1 2  f
2 5  g
3 7  f

wanted:  a function which returns c(f(2),g(5),f(7)) = c(34, 15, 119)

A simple application of do.call doesn't do what I need (it applies the
function f to each x): 

> do.call(foo$fn,args=list(x = foo$x))
[1]  34  85 119

Whereas the following works but seems like overkill.

> diag(sapply(foo$fn,do.call,args=list(x=foo$x)))
[1]  34  15 119

Another idea that didn't work:

 do.call("evalq",args =list(paste(foo$fn,"(foo$x)",sep="")))
[1] "f(foo$x)" "g(foo$x)" "f(foo$x)"

Thanks in advance
Anne

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anne E. York
National Marine Mammal Laboratory
Seattle WA 98115-0070  USA
e-mail: anne.york at noaa.gov
Voice: +1 206-526-4039
Fax: +1 206-526-6615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




More information about the R-help mailing list