[R] Execute a function
Bert Gunter
gunter.berton at gene.com
Fri Jul 20 16:10:15 CEST 2012
Inline.
-- Bert
On Fri, Jul 20, 2012 at 6:59 AM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> On 2012-07-20 04:05, carla moreira wrote:
>>
>>
>> Hi,
>>
>> I would like to evaluate a function, with 3 arguments, for instance,
>>
>> myfunc<-function(a,b,c) { sqrt(a)-exp(b)+4*c
>> }
>>
>> How to execute myfunc(x,y,z), for all x, all y and all z, where x,y,z are
>> vectors?
>
>
> Is this what you have in mind:
>
> myfunc <- function(a, b, c){ sqrt(a)-exp(b)+4*c }
> myfunc2 <- function(x){
> a <- x[1]
> b <- x[2]
> c <- x[3]
> myfunc(a, b, c)
> }
>
> x <- c(1, 4, 9)
> y <- 1:2
> z <- c(10, -10, 2, 20)
> d <- expand.grid(x, y, z)
Peter, what's wrong with
with(d,myfunc(x,y,z)) ??
I realize this depends on the function be vectorizable, but isn't that
the point? It could be orders of magnitude faster than looping via
apply.
-- Bert
> d$value <- apply(d, 1, myfunc2)
>
> ?
>
> Peter Ehlers
>
>>
>> Thank you very much in advance
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Execute-a-function-tp4637182.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at 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.
>>
>
> ______________________________________________
> R-help at 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list