【发布时间】:2015-10-23 11:26:44
【问题描述】:
注意-相关问题没有解释为什么attr(myVar, "class") 返回NULL 而不是list(以及为什么class(myVar) 返回list)。 R中检查对象类的标准方法是什么?
如何确定一个对象是“我的自定义类”还是“我的自定义类”对象的“列表”?
foo <- function(x) {
a=list(x=x)
attr(a, "class") <- "myclass"
return(a)
}
newVar = list(foo(10),foo(20))
现在我想知道newVar是什么类。
attr(newVar, "class") # NULL, but not list!
#NULL
##however this works fine
attr(newVar[[1]], "class")
#[1] "myclass"
为什么会这样?在 R 中确定类的正确方法是什么?
【问题讨论】:
-
试试
class(newVar) -
class(newVar)给出list。 -
您能解释一下
attr(myVar, "class")和class(myVar)之间有什么区别吗?我们应该什么时候使用?