To iterate over a list in R, you can use a for loop or apply functions like lapply or sapply.
In this example,
numbers
with numeric elements.numbers <- c(10, 20, 30, 40, 50)
for (num in numbers) {
print(num)
}
10 20 30 40 50
In this example,
numbers
with numeric elements.lapply
function to apply a function (e.g., print) to each element in the list.numbers <- c(10, 20, 30, 40, 50)
lapply(numbers, print)
[1] 10 [1] 20 [1] 30 [1] 40 [1] 50
In this tutorial, we learned How to Iterate Over a List in R language with well detailed examples.