1 | initial version |
You can add additional columns to a matrix that consists of lists in several ways. Here are two examples:
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".
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.