我想使用nrow函数来表示我将要定义的类略有不同.但我也不想掩盖nrow函数,所以我想重新定义nrow as
nrow <- function(x) UseMethod("nrow")
nrow.matrix <- function(x) base::nrow(x)
nrow.data.frame <- function(x) base::nrow(x)
nrow.list <- function(x) base::nrow(x) # should return NULL
nrow.numeric <- function(x) base::nrow(x) # should return NULL
nrow.character <- function(x) base::nrow(x) # should return NULL
nrow.ts <- function(x) base::nrow(x) # should return NULL
Run Code Online (Sandbox Code Playgroud)
这是什么东西,犹太教?这是错误的方法吗?
只需定义一个默认方法:
x <- 1
class(x) <- "myclass"
nrow <- function(x) UseMethod("nrow")
nrow.default <- base::nrow
nrow.myclass <- function(x) 42
nrow(x)
#[1] 42
nrow(matrix(1:15, ncol=3))
#[1] 5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |