0
$\begingroup$

This is something I have been wondering for ages but I am never able to get an answer.

I am trying to understand how to make a dataframe in R, where each element of the dataframe is itself a vector or a matrix.

For example, lets say we have a regular vector V with elements being real numbers.

Then to acess any number we would have

V[3] which would give the third element of said vector.

Now I want to know how to do this with say a dataframe D,

where each element of the dataframe is itself a vector or matrix, so that say

D[3] is not a real number, but a vector.

How can this be done in R?

Thanks all

$\endgroup$
0
$\begingroup$

Dataframe is a representation of 2 D matrix in R. R does not support MultiIndex dataframes that can represent more complex structures.

Maybe you can model the problem as a Tensor ( https://cran.r-project.org/web/packages/tensorr/vignettes/introduction.html , https://www.rdocumentation.org/packages/rTensor/versions/1.4/topics/Tensor-class)

$\endgroup$
0
$\begingroup$

This can be done like this:

library(data.table)

dt<-data.table(x=c("a","b","c"), y= lapply(1:3, function(x) matrix(rep(x, x*x), nrow=x)))

dt[2, y]

#[[1]]
#     [,1] [,2]
#[1,]    2    2
#[2,]    2    2

dt[2, `:=`(y=list(c(1,2,3)))]
dt[2,y]

#[[1]]
#[1] 1 2 3


```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.