r - Variable matrix index and row/column as indices in a single function argument -
how handle variable matrix index , row/column indices in single function argument?
m <- matrix(1:9, 3) fn <- function(m, subsetarg) { stopifnot(m[subsetarg] == 6) }
i'd able use both situations:
a <- matrix(false, 3, 3) a[2,3] <- true # yielding # f f f # f f t # f f f fn(m, subsetargument = a) # works
and
fn(m, subsetargument = tuple(2,3)) # <- not work logically
note after using range, example tuple(2, 1:3)
i understand done explicitly testing either 1 or 2 variables given, feel there might easier way.
just slurp arguments , pass them call [
:
fn <- function(...) { stopifnot(do.call(`[`, list(...)) == 6) }
everything in r function, including subsetting :-)
Comments
Post a Comment