I'm trying to store an entire matrix/array into a single cell of a data frame, but can't quite remember how to do it.
Now before you say it can't be done, I'm sure I remember someone asking a question on SO where it was done, although that wasn't the point of the question so I can't find it again.
For example, you can store matrices inti a single cell of a matrix like so:
myMat <- array(list(), dim=c(2, 2))
myMat[[1, 1]] <- 1:5
myMat[[1, 2]] <- 6:10
# [,1] [,2]
#[1,] Integer,5 Integer,5
#[2,] NULL NULL
The trick was in using the double brackets [[]]
.
Now I just can't work out how to do it for a data frame (or if you can):
# attempt to make a dataframe like above (except if I use list() it gets
# interpreted to mean the `m` column doesn't exist)
myDF <- data.frame(i=1:5, m=NA)
myDF[[1, 'm']] <- 1:5
# Error in `[[<-.data.frame`(`*tmp*`, 1, "m", value = 1:5) :
# more elements supplied than there are to replace
# this seems to work but I have to do myDF$m[[1]][[1]] to get the 1:5,
# whereas I just want to do myDF$m[[1]].
myDF[[1, 'm']] <- list(1:5)
I think I'm almost there. With that last attempt I can do myDF[[1, 'm']]
to retrieve list(1:5)
and hence myDF[[1, 'm']][[1]]
to get 1:5
, but I'd prefer to just do myDF[[1, 'm']]
and get 1:5
.