Search code examples
rlistreverse

How to reverse a list in R?


I'm trying to reverse a list with function list.reverse. I run an example from its documentation.

x <- list(a=1,b=2,c=3)
list.reverse(x)

R returns an error message "Error in list.reverse(x) : could not find function "list.reverse" ".


Solution

  • The function does work. You have not loaded or installed rlist.

    Try :

    rlist::list.reverse(x)
    
    #$c
    #[1] 3
    
    #$b
    #[1] 2
    
    #$a
    #[1] 1
    

    and so does base R rev works :

    rev(x)
    
    #$c
    #[1] 3
    
    #$b
    #[1] 2
    
    #$a
    #[1] 1