Skip to content
Prev 165027 / 398506 Next

passing arguments to subset from a function

Available free for the typing are the functions for the default and  
the dataframe methods of subset:

 >  subset.default
function (x, subset, ...)
{
     if (!is.logical(subset))
         stop("'subset' must be logical")
     x[subset & !is.na(subset)]
}


 > subset.data.frame
function (x, subset, select, drop = FALSE, ...)
{
     if (missing(subset))
         r <- TRUE
     else {
         e <- substitute(subset)
         r <- eval(e, x, parent.frame())
         if (!is.logical(r))
             stop("'subset' must evaluate to logical")
         r <- r & !is.na(r)
     }
     if (missing(select))
         vars <- TRUE
     else {
         nl <- as.list(1:ncol(x))
         names(nl) <- names(x)
         vars <- eval(substitute(select), nl, parent.frame())
     }
     x[r, vars, drop = drop]
}

(There is also a matrix method.)