R is giving the following message error when you want to save an S4 object into a list of list and the element was not already defined previously.
"invalid type/length (S4/0) in vector allocation"
Why is it working with a simple list, but not with a list of list?
See the following code and the potential workarounds. However, I am pretty sure there is a more obvious solution.
# 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
Any suggestion on why it does not work with a list of list and how I can solve this problem? Thanks