[R] Using subset() in a user-defined function

From: Tobin, Jared <TobinJR_at_DFO-MPO.GC.CA>
Date: Thu, 14 Jun 2007 11:08:41 -0230


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.



jared tobin, student research assistant
dept. of fisheries and oceans

R-help_at_stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 14 Jun 2007 - 13:45:46 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 Thu 14 Jun 2007 - 14:32:00 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.