0
我嘗試編寫一個函數,在該函數中我可以拋出不定數量的對象並獲取該對象的數據類型列表。這是學習S3 Generics的一項個人任務。使用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完全陌生,所以我可能會做一些基本錯誤的事情。
感謝您的幫助和良好的解釋!這幫助我很多! – ruedi