【发布时间】:2015-05-22 07:08:27
【问题描述】:
我尝试编写一个函数,我可以在其中输入任意数量的对象并获取该对象的数据类型列表。这是学习 S3 泛型的个人任务。
到目前为止我所做的是:
myTypes <- function(x, ...) {
dots <- list(...)
return (as.list(apply(dots, 1, myType)))
}
myType <- function(x){
UseMethod("myType")
}
myType.numeric <- function(x){
if(is.numeric(x)) "Type: numberic"
}
myType.data.frame <- function(x){
if(is.data.frame(x)) "Type: dataframe"
}
发生错误,例如当我打电话时
x <- 1
y <- 3
myTypes(x,y)
我总是收到错误:"Error in apply(dots, 1, myType) : dim(X) must have a positive length",我不确定出了什么问题。有人可以在这里帮助我吗?由于我对 R 完全陌生,所以我可能做错了什么。
【问题讨论】: