An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110401/b9f55e6b/attachment.pl>
function in argument
4 messages · David Winsemius, array chip, Henrique Dallazuanna
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110401/71889282/attachment.pl>
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