R – Reverse a List
To reverse a list in R programming, call rev() function and pass given list as argument to it. rev() function returns returns a new list with the contents of given list in reversed order.
The syntax to reverse a list x
is
</>
Copy
rev(x)
Return Value
The rev() function returns a list.
Examples
In the following program, we take a list in x
, and reverse this list using rev().
example.R
</>
Copy
x <- list("a", "b", "c")
result = rev(x)
print(result)
Output
[[1]]
[1] "c"
[[2]]
[1] "b"
[[3]]
[1] "a"
Now, let us take a list x
with numeric values and reverse it.
example.R
</>
Copy
x <- list(5, 25, 125)
result = rev(x)
print(result)
Output
[[1]]
[1] 125
[[2]]
[1] 25
[[3]]
[1] 5
Conclusion
In this R Tutorial, we learned how to reverse a list using rev() function, with the help of examples.