[R] Using subset() in a user-defined function
Tobin, Jared
TobinJR at DFO-MPO.GC.CA
Thu Jun 14 17:15:08 CEST 2007
I should mention the idea I worked on yesterday was cat()ing together
the appropriate condition strings and then ideally printing that entire
concatenated string into the subset argument. However, as far as I
know, cat() only prints directly on the console and cannot be used to
substitute input text into a function, so I scrapped that idea.
Just figured I'd mention that in case there does happen to be a way to
do such a thing, and someone knows of a way offhand.
--
jared tobin, student research assistant
dept. of fisheries and oceans
tobinjr at dfo-mpo.gc.ca
-----Original Message-----
From: Tobin, Jared
Sent: Thursday, June 14, 2007 12:28 PM
To: 'Duncan Murdoch'
Cc: r-help at stat.math.ethz.ch
Subject: RE: [R] Using subset() in a user-defined function
Thanks for the quick response, Duncan.
The given code doesn't seem to work, and possibly due to this reason I
found in the online help for missing() (if I understand it correctly):
"Currently missing() can only be used in the immediate body of the
function that defines the argument, not in the body of a nested function
or a local call. This may change in the future."
So as I understand it, missing() cannot refer to the arguments of
function1 if it is used in an argument of subset()? It seems to remain
a promising function for this situation regardless, but I'm not sure how
I could implement it into the subset() arguments offhand.
--
jared tobin, student research assistant
dept. of fisheries and oceans
tobinjr at dfo-mpo.gc.ca
-----Original Message-----
From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca]
Sent: Thursday, June 14, 2007 11:28 AM
To: Tobin, Jared
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Using subset() in a user-defined function
On 6/14/2007 9:38 AM, Tobin, Jared wrote:
> Hello,
>
> I'm having a problem with using subset() inside a function I'm
writing.
> Ignoring everything else in the function, the problem can be
> illustrated by (where master.frame is the data frame I'm using):
>
>
> function1 <- function(arg1="", arg2="", arg3=""){
>
> temp.frame <- subset(master.frame, a == arg1 & b == arg2 & c ==
> arg3)
>
> }
>
>
> This works fine if the user specifies all arguments, but if any one or
> more of the arguments isn't specified, say arg1 for example, the
> subset is empty because subset() goes looking for values of a == "" in
> master.frame, and there are none. I want it to work such that if an
> argument is not specified, it is not included in what subset() goes
> looking for. So if I were to input:
>
> function1(arg2=5, arg3=6)
>
> then in function1, the subset command will look like
>
> temp.frame <- subset(master.frame, b == 5 & c == 6)
>
>
> Any suggestions would be much appreciated.
Code it like this:
subset(master.frame, (missing(arg1) | a == arg1) &
(missing(arg2) | b == arg2) &
(missing(arg3) | c == arg3))
I haven't tried this, and I forget what happens in subset() if you pass
it a subset of the wrong length, so it might fail if all args are
missing, but otherwise I think it should work. It does depend on
defaults for the args existing and not causing errors in the equality
tests (it's not using shortcut evaluation).
Duncan Murdoch
More information about the R-help
mailing list