[R] R functions with array arguments

baptiste auguie baptiste.auguie at googlemail.com
Thu Sep 17 10:39:55 CEST 2009


Try this,

sapply(Mabslim , my_gamma, alpha=-1, xstar = -21, xmax = -27)

or wrap it with ?Vectorize,

vmy_gamma = Vectorize(my_gamma, vectorize.args = "xlim")
vmy_gamma(alpha=-1, xstar = -21, xlim= Mabslim, xmax = -27)


HTH,

baptiste


2009/9/17 Maurizio Paolillo <paolillo at na.infn.it>:
> Dear R users,
> I'm trying to implement a self-defined function with multiple
> arguments, one of which is an array, but I find that the result is a
> single value instead of an array.
> This is the example I'm working on:
>
>
> # define integration limit vector
>  > Mabslim <- c(-17.95, -16.65, -17.27, -17.62, -16.76, -17.07, -17.02)
>
> # Define function
>  > schech<-function(x,alpha,xstar) (10^(0.4*(alpha+1)*(xstar-
> x))*exp(-10^(0.4*(xstar-x))))
>
> # Define new function integrating the previous one
>  > my_gamma<-function(alpha,xstar,xlim,xmax)
> integrate(schech,xmax,xlim,alpha,xstar)$value
>
>  > my_gamma(-1,-21,Mabslim,-27)
> [1] 2.487746
>
> Note that 'Mabslim' is used as an upper integration limit within the
> function. I tried to use "sapply(....)" but it looks to me as if this
> can be used only if the array is the first argument of the function.
> I'm a beginner to R, apologies if I'm just using the wrong approach.
> Any suggestion on how to solve this?
> Thanks,
> Maurizio
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list