How to make a dataframe with lists or vectors as its elements

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

Topic code data r

Category Data Science


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


```

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)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.