Create a matrix with vector (not scalar) elements

Viewed 52

I want to create an object like a matrix with vectors for its elements. like:

(1,2,3) (1,3,6) (2,4,1)

(0,7,8) (2,3,4) (5,2,1)

(9,0,8) (8,4,6) (1,1,1)

What should I do?

1 Answers

You can use a matrix list. Initially you create a list of vectors:

vec <- list(1:3, 4:10, 2:5, letters[1:3])

then coerce it to a matrix:

mat1 <- matrix(vec, nrow = 2, ncol = 2)
#     [,1]      [,2]       
#[1,] integer,3 integer,4  
#[2,] integer,7 character,3

mat2 <- matrix(vec, nrow = 2, ncol = 2, byrow = TRUE)
#     [,1]      [,2]       
#[1,] integer,3 integer,7  
#[2,] integer,4 character,3

To extract its elements, use for example:

mat1[1, 1][[1]]

The first [1, 1] is getting matrix element. But since an element is a list, use an extra [[1]] to access the actual vector.


If you want to see its contents when printing it, then coerce it to a data frame instead. For example,

dat1 <- data.frame(mat1)
#                    X1         X2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

You can change column names later, like,

names(dat1) <- c("col1", "col2")
#                  col1       col2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

Accessing its columns by dat1$col1, dat1[[1]] or dat1[["col1"]]. In this case, each column is a list.

If your end goal is a data frame not a matrix, you can construct this data frame directly, but has to protect each column with I():

data.frame(col1 = I(list(1:3, 4:10)),
           col2 = I(list(2:5, letters[1:3])) )
#          col1       col2
#1      1, 2, 3 2, 3, 4, 5
#2 4, 5, 6,....    a, b, c

Anyway, I don't like these data structures. It potentially make further operation cumbersome.

Related