Create Nested List in R (2 Examples) | for-Loop, list, length & get Functions Explained in RStudio

preview_player
Показать описание
R code of this video:

list_1 <- list(12:20, # Create first example list
letters[16:11],
"yyyy")
list_1 # Print first example list

list_2 <- list(4:8, # Create second example list
letters[7:1],
"xxx")
list_2 # Print second example list

list_3 <- list("Another", # Create third example list
"list",
"in R")
list_3 # Print third example list

my_nested_list1 <- list(list_1, # Create nested list using list()
list_2,
list_3)
my_nested_list1 # Print nested list

my_list_names <- c("list_1", "list_2", "list_3") # Create vector of list names
my_list_names # Print vector of list names

my_nested_list2 <- list() # Create empty list
my_nested_list2 # Print empty list

for(i in 1:length(my_list_names)) { # Run for-loop over lists
my_nested_list2[[i]] <- get(my_list_names[i])
}

my_nested_list2 # Print nested list

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Hi, Thanks for sharing all the informative videos.
I have 5 variables in outer loop and 5 variables in the inner loop. I want to pick one- one from each.
I was exploring how we can write R code for nested loop to pick first from outer loop and then first from inner loop. Once this is done then second from outer loop and then second from inner loop.
So far loop is picking one from outer loop and taking 1:5 from inner loop. Once it is done, then going to second variable in outer look. Thank you in advance.

kanhabira
welcome to shbcf.ru