Skip to content
Back to formatted view

Raw Message

Message-ID: <065AB2E1-BD10-4CD0-9B20-FD1B25B183F3@comcast.net>
Date: 2011-04-01T22:06:43Z
From: David Winsemius
Subject: function in argument
In-Reply-To: <463943.82411.qm@web125807.mail.ne1.yahoo.com>

On Apr 1, 2011, at 5:56 PM, array chip wrote:

> 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"
>

Works on my machine.

 > foo(matrix(1:100,nrow=5))
           1         2         3         4
2  4.472136
3  8.944272  4.472136
4 13.416408  8.944272  4.472136
5 17.888544 13.416408  8.944272  4.472136


You have probably overwritten `dist` with a non-functional object.

Try rm(dist) and re-run:


>
> foo<-function (x,
>     xfun = mean)
> {
>  xfun(x)
> }
>
> foo(1:10)
> [1] 5.5
>
> what am I missing here?

Thinking about your full workspace, I would guess.

--

David Winsemius, MD
West Hartford, CT