Re: [R] function in argument

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Fri, 01 Apr 2011 19:10:46 -0300

Or:

foo<-function (x, xfun = dist) {

    match.fun(xfun)(x)
}

On Fri, Apr 1, 2011 at 7:06 PM, array chip <arrayprofile_at_yahoo.com> wrote:
> OK, I figured it out, need to add stats::: before dist
>
> foo<-function (x,
>    xfun = stats:::dist)
> {
> xfun(x)
> }
>
>
> John
>
>
> ________________________________
>
> To: r-help_at_r-project.org
> Sent: Fri, April 1, 2011 2:56:06 PM
> Subject: [R] function in argument
>
> Hi, I tried to pass the function dist() as an argument, but got an error
> message. However, almost the same code with mean() as the function to be passed,
>
> it works ok.
>
> foo<-function (x,
>    xfun = dist)
> {
> xfun(x)
> }
>
> foo(matrix(1:100,nrow=5))
> Error in foo(matrix(1:100, nrow = 5)) : could not find function "xfun"
>
>
> foo<-function (x,
>     xfun = mean)
> {
>  xfun(x)
> }
>
> foo(1:10)
> [1] 5.5
>
> what am I missing here?
>
> Thanks
>
> John
>
>    [[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.
>
>        [[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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
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.
Received on Fri 01 Apr 2011 - 22:16:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Apr 2011 - 22:20:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive