[R] S4 method selection based on second argument
Gabor Grothendieck
ggrothendieck at myway.com
Wed Sep 29 00:40:43 CEST 2004
Paul Roebuck <roebuck <at> odin.mdacc.tmc.edu> writes:
:
: I'm translating some Matlab code and need some help figuring
: out how to change this call into an S4 generic method.
:
: In matlab, there's a function called 'repmat' with three
: calling sequences (all I have to deal with anyway):
: 1) B = repmat(A, m, n)
: 2) B = repmat(A, [m n])
: 3) B = repmat(A, n)
: In all cases, A is the fill value, m is number of rows,
: and n is number of columns.
:
: As separate functions, the translations would roughly be:
:
: repmat1 <- function(A, m, n) {
: kronecker(matrix(1, n, m), A)
: }
:
: repmat2 <- function(A, rc) {
: repmat1(A, rc[1], rc[2])
: }
:
: repmat3 <- function(A, n) {
: repmat1(A, n, n)
: }
:
: Suggestions?
This can be done easily without any oo dispatch like this:
repmat <- function(A, m, n = if (length(m) == 1)m) {
m <- c(m, n)
stopifnot(length(m) == 2)
kronecker(matrix(1,m[1],m[2]),A)
}
More information about the R-help
mailing list