0

I would like a "2-dimensional list" where entry (i, j) stores a vector/matrix of unknown size. Something like

my2Dlist[i,j] <- runif(k) 

where k varies with i and j. What is the data structure I am looking for?

Thank you.

4
  • 3
    The components of dataframes can be lists containing anything (e.g. matrices or vectors) Commented Apr 17, 2019 at 19:25
  • @Cettt Could you explain further? I tried creating an empty 2x2 data frame test <-data.frame(matrix(ncol=2,nrow=2)) and then test[1,1] <- c(1,1), but R didn't dig it... Commented Apr 17, 2019 at 19:53
  • I do not see how a data frame satisfies my needs. The columns of a data frame can be heterogeneous, but I cannot (or cannot see how to) store a matrix in mydataframe[i,j]. Commented Apr 17, 2019 at 20:42
  • Eventually you can use a list of lists with my2Dlist[[i]][[j]] to access an element at the position (i, j). Commented Apr 18, 2019 at 4:41

1 Answer 1

5

Self-Answer: The solution I found is as follows. If I want a 2x2 list, I write

my2Dlist <- as.list(1:4) dim(my2Dlist) <- c(2,2) 

One can then store anything in the components, for example

my2Dlist[[1,2]] <- c(1, 1, 2, 3, 5, 8) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.