How to name the list after creating the list in R?

r

#1

I am currently studying the list in R .I have created a list using three different type of objects one is a vector , the second is a matrix and the last one is a data frame.

my_vector <- 1:10 

my_matrix <- matrix(1:9, ncol = 3)

my_df <- mtcars[1:10,]

my_list <- list(my_vector, my_matrix, my_df)

I want to know is there a way by which I can name my list components after creating my list.


#2

Hello @harry,

my_list$ will give the elements v,m and df!

Hope this helps!!


#3

There is another way to specify the list components using names() command after the list is created.
names(my_list) <- c(“v”, “m”, “df”)

Then one can print

names(my_list)
[1] “v” “m” "df"
my_list$v
[1] 1 2 3 4 5 6 7 8 9 10
my_list$m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9