首页 > 解决方案 > 将 S4 对象保存在列表列表中

问题描述

当您要将 S4 对象保存到列表列表中并且之前尚未定义该元素时,R 会给出以下消息错误。

"invalid type/length (S4/0) in vector allocation"

为什么它适用于简单列表,而不适用于列表列表?

请参阅以下代码和潜在的解决方法。但是,我很确定有一个更明显的解决方案。

# Creation of an S4 object
setClass("student", slots=list(name="character", age="numeric", GPA="numeric"))
s <- new("student",name="John", age=21, GPA=3.5)

# Indexes for the list
index1 <- "A"
index2 <- "a"

# Simple list (All of this works)
l <- list()
l[[index1]] <- s
l[[index1]] <- "character"
l[[index1]] <- 999


# List of list 
l <- list()
l[[index1]][[index2]] <- s          # will give an Error!!
l[[index1]][[index2]] <- "character" # still working
l[[index1]][[index2]] <- 999         # still working


# "Workarounds"
l <- list()
l[[index1]][[index2]] <- rep(999, length(slotNames(s))) #define the element with a length equal to the number of slots in the s4 object
l[[index1]][[index2]] <- s # this works now!


l[[index1]][[index2]] <- list(s) # This works too, but that's not the same result

关于为什么它不适用于列表列表以及如何解决此问题的任何建议?谢谢

标签: rlists4

解决方案


所以当你这样做时

l <- list()
l[[index1]][[index2]] <- s

问题是它l被初始化为一个列表,所以用 设置一个新的命名元素是有意义的l[[index1]],但是 R 不知道存储在什么l[[index1]][[index2]]。它可以是任何东西。它可能是一个函数,而函数不知道如何处理命名索引操作。例如

l <- list()
l[[index1]] <- mean
l[[index1]][[index2]] <- "character"

但是在您的情况下,当您尝试从尚未初始化的列表中获取值时,您将获得NULL. 例如

l <- list()
l[[index1]]
# NULL

当您尝试在 NULL 对象上设置命名原子值时,R 恰好具有特殊行为。观察

# NULL[["a"]] <- "character" is basically calling....
`[[<-`(NULL, "a", "character")
#           a 
# "character" 

请注意,我们在这里得到了一个命名向量。不是清单。这对于您的“工作”示例也是如此

l <- list()
l[[index1]][[index2]] <- "character"
class(l[[index1]][[index2]])
# [1] "character"

另请注意,这与 S4 没有任何关系。如果我们也尝试设置更复杂的对象(如函数),也会发生同样的情况

l <- list()
l[[index1]][[index2]] <- mean
# Error in l[[index1]][[index2]] <- mean : 
#   invalid type/length (closure/0) in vector allocation

在像 Perl 这样的语言中,您可以通过autovivification使用正确的索引语法“神奇地”将哈希带入生活,但在 R 中并非如此。如果您希望 alist()存在,l[[index1]]则需要显式创建它。这将起作用

l <- list()
l[[index1]] <- list()
l[[index1]][[index2]] <- s

同样,这是因为[[ ]]在 R 中有点模棱两可。它是一个通用索引函数,不专门用于列表。


推荐阅读