Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can add additional columns to a matrix that consists of lists in several ways. Here are two examples:

  1. Using cbind() function: Suppose that you have a matrix "my_matrix" consisting of two lists "list1" and "list2" as below:
my_matrix <- matrix(list(list1=c(1,2,3), list2=c("a","b","c")), nrow=3)

To add an additional column to "my_matrix" consisting of a new list "list3", you can use the cbind() function as follows:

new_list <- list("d", "e", "f")
my_matrix <- cbind(my_matrix, new_list)

This will add the new list "newlist" as a new column to the matrix "mymatrix".

  1. Using matrix indexing: Suppose that you have a matrix "my_matrix" consisting of two lists "list1" and "list2" as below:
my_matrix <- matrix(list(list1=c(1,2,3), list2=c("a","b","c")), nrow=3)

To add an additional column to "my_matrix" consisting of a new list "list3", you can use matrix indexing as follows:

new_list <- list("d", "e", "f")
my_matrix <- cbind(my_matrix, list())
my_matrix[,3] <- new_list

This will add a new, empty column to "mymatrix" using cbind() function first, and then populate the column with the new list "newlist" using matrix indexing.