我对 S3 很陌生,想知道是否有可能给出以下数据框:
test <- tibble(
elements = c("one", "two", "three"),
S3 = c("foo", "bar", "foo")
)
我可以给列中的每个元素elements
一个来自 S3 列的自定义类:
custom_class <- function(x, customclass) {
class(x) <- c(class(x), customclass)
return(x)
}
# test
s <- "string"
custom_class(s, "anything")
test <- tibble(
elements = c("one", "two", "three"),
S3 = c("foo", "bar", "foo"),
testing = custom_class(elements, S3)
)
但这不起作用。这是由于我对 S3 的理解存在心理模型差距吗?是否可以以这种方式对每个元素应用不同的类,如果不能,可以以某种方式将输出创建为列表,以便列表中的每个元素都是一个element
with class S3
?任何提示/帮助建议表示赞赏!