An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081210/05926816/attachment.pl>
missing argument
5 messages · jonas garcia, Tony, Bert Gunter
?missing never used it myself, but looks like it might help you :-) Tony Breyal. On 10 Dec, 19:09, "jonas garcia" <garcia.jona... at googlemail.com> wrote:
Dear list,
I have a question and I'm going to give an example of my problem
f<- function(d1, d2, d3)
{
d<- d1*d2/d3
return(d)
}
v1<- 1
v2<- 2
If I try
f(v1, v2, v3)
Error in f(v1, v2, v3) : object "v3" not found
I obviously got the above error message.
I would like to add something to my function to allow me to get a certain
value (say zero)
if one of the arguments is not provided (in this case v3).
Thanks in advance
J
? ? ? ? [[alternative HTML version deleted]]
______________________________________________ R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
I think i might of misunderstood the question. I was thinking you
meant what if one of the arguments were not used, as in the example
below (see ?missing):
f<- function(d1, d2, d3)
{
vec <- c(missing(d1), missing(d2), missing(d3))
if(any(vec)) {
return(0)
} else {
d<- d1*d2/d3
return(d)
}
}
v1 <- 1
v2 <- 2
f(v1, v2)
[1] 0
however, if you meant that you wanted the 0 returned if one of the
variables passed does not exist, maybe the following is what you want?
(see ?try)
f<- function(d1, d2, d3)
{
d <- try(d1*d2/d3, silent=TRUE)
if(class(d) == "try-error") {
return(0)
} else {
return(d)
}
}
v1 <- 1
v2 <- 2
f(v1, v2, var_that_does_not_exist)
[1] 0
appologies if i missunderstood the question, but hopefully the above
is a little helpful :-)
Tony Breyal
On 10 Dec, 19:09, "jonas garcia" <garcia.jona... at googlemail.com>
wrote:
Dear list,
I have a question and I'm going to give an example of my problem
f<- function(d1, d2, d3)
{
d<- d1*d2/d3
return(d)
}
v1<- 1
v2<- 2
If I try
f(v1, v2, v3)
Error in f(v1, v2, v3) : object "v3" not found
I obviously got the above error message.
I would like to add something to my function to allow me to get a certain
value (say zero)
if one of the arguments is not provided (in this case v3).
Thanks in advance
J
? ? ? ? [[alternative HTML version deleted]]
______________________________________________ R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
I believe the usual practice in this case is simply to give default values for arguments: function(x, y, opt.arg1 = 0, opt.arg2 = sin(1),...) If you haven't already done so, perusal of "An Introduction to R" -- especially the "Named Arguments and Defaults" -- would be appropriate. Cheers, Bert Gunter -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Tony Breyal Sent: Wednesday, December 10, 2008 1:01 PM To: r-help at r-project.org Subject: Re: [R] missing argument ?missing never used it myself, but looks like it might help you :-) Tony Breyal. On 10 Dec, 19:09, "jonas garcia" <garcia.jona... at googlemail.com> wrote:
Dear list,
I have a question and I'm going to give an example of my problem
f<- function(d1, d2, d3)
{
d<- d1*d2/d3
return(d)
}
v1<- 1
v2<- 2
If I try
f(v1, v2, v3)
Error in f(v1, v2, v3) : object "v3" not found
I obviously got the above error message.
I would like to add something to my function to allow me to get a certain
value (say zero)
if one of the arguments is not provided (in this case v3).
Thanks in advance
J
? ? ? ? [[alternative HTML version deleted]]
______________________________________________ R-h... at r-project.org mailing
listhttps://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting
guidehttp://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.
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081212/2a785798/attachment.pl>