How do I create a for loop that iterates through a list in R?

info_list <- list(
Esther = list(
jersey = 3432,
color = “purple”
),
Feng = list(
jersey = 4221,
color = “blue”
),
Megan = list(
jersey = 1363,
color = ‘green’
),
Janet = list(
jersey = 6729,
color = ‘green’
),
Tina = list(
jersy = 7501,
color = ‘orange’
)
)


```{r}
print_information <- function(name) {
  print(paste(name, "is #", info_list[[name]]$jersey, "wearing the color", info_list[[name]]$color))
}

loop <- for (friend in info_list) {
  print_information(friend)
}

loop()

# call the print_information function on the friends vector:
print_information('Megan')
print_information('Janet')
print_information('Tina')

The function print_information was provided, along with the data about the people. I wrote the code that calls print_information for Megan, Janet, and Tina at the bottom. The next exercise is to create a for loop that iterates through the info list and calls print_information for each person. I tried to write the loop, but the information wasn’t showing up when I ran the code. Any suggestions?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.