To iterate over a list in R in reverse order, you can use the rev() function.
In this example,
numbers
with numeric elements.rev()
function to iterate over the vector in reverse order.numbers <- c(1, 2, 3, 4, 5)
for (number in rev(numbers)) {
print(number)
}
5 4 3 2 1
In this example,
names
with string elements.names <- c("Alice", "Bob", "Charlie", "David")
for (i in length(names):1) {
print(names[i])
}
David Charlie Bob Alice
In this tutorial, we learned How to Iterate Over a List in Reverse Order in R language with well detailed examples.