Rev provides a reversed version of its argument. Unlike the basic function, it does in higher-dimensional structures such as matrices not reverse the elements, but the order of the rows and/or columns. It further offers additional interfaces for higher dimensional arrays or tables.

Rev(x, ...)

# S3 method for matrix
Rev(x, margin, ...)

# S3 method for table
Rev(x, margin, ...)

# S3 method for array
Rev(x, margin, ...)

# S3 method for data.frame
Rev(x, margin, ...)

Arguments

x

a vector, a matrix or a higher dimensional table to be reversed.

margin

vector of dimensions which to be reversed (1 for rows, 2 for columns, etc.). If not defined, all dimensions will be reverted.

...

the dots are passed to the array interface.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

tab <- matrix(c(1, 11, 111,
                2, 22, 222,
                3, 33, 333), 
              byrow=TRUE, nrow=3,
              dimnames=list(mar1=1:3, mar2=c("a","b","c")))

Rev(tab, margin=1)
#>     mar2
#> mar1 a  b   c
#>    3 3 33 333
#>    2 2 22 222
#>    1 1 11 111
Rev(tab, margin=2)
#>     mar2
#> mar1   c  b a
#>    1 111 11 1
#>    2 222 22 2
#>    3 333 33 3

# reverse both dimensions
Rev(tab, margin=c(1, 2))
#>     mar2
#> mar1   c  b a
#>    3 333 33 3
#>    2 222 22 2
#>    1 111 11 1

t(tab)
#>     mar1
#> mar2   1   2   3
#>    a   1   2   3
#>    b  11  22  33
#>    c 111 222 333

# reverse 3dimensional array
aa <- Abind(tab, 2 * tab, along=3)
dimnames(aa)[[3]] <- c("A","Z")

# reverse rows
Rev(aa, 1)
#> , , A
#> 
#>   a  b   c
#> 3 3 33 333
#> 2 2 22 222
#> 1 1 11 111
#> 
#> , , Z
#> 
#>   a  b   c
#> 3 6 66 666
#> 2 4 44 444
#> 1 2 22 222
#> 
# reverse columns
Rev(aa, 2)
#> , , A
#> 
#>     c  b a
#> 1 111 11 1
#> 2 222 22 2
#> 3 333 33 3
#> 
#> , , Z
#> 
#>     c  b a
#> 1 222 22 2
#> 2 444 44 4
#> 3 666 66 6
#> 
# reverse 3th dimension
Rev(aa, 3)
#> , , Z
#> 
#>   a  b   c
#> 1 2 22 222
#> 2 4 44 444
#> 3 6 66 666
#> 
#> , , A
#> 
#>   a  b   c
#> 1 1 11 111
#> 2 2 22 222
#> 3 3 33 333
#> 

# reverse all dimensions
Rev(aa)
#> , , Z
#> 
#>     c  b a
#> 3 666 66 6
#> 2 444 44 4
#> 1 222 22 2
#> 
#> , , A
#> 
#>     c  b a
#> 3 333 33 3
#> 2 222 22 2
#> 1 111 11 1
#> 
# same as
Rev(aa, margin=(1:3))
#> , , Z
#> 
#>     c  b a
#> 3 666 66 6
#> 2 444 44 4
#> 1 222 22 2
#> 
#> , , A
#> 
#>     c  b a
#> 3 333 33 3
#> 2 222 22 2
#> 1 111 11 1
#>