reverse an array in r

To reverse an array in R, we can use the sequence() function to generate a sequence of index numbers in reverse order, and then use the generated sequence to index the elements of the original array. Here's a code snippet to demonstrate how to reverse an array in R:

main.r
# create an example array
my_array <- c(1, 2, 3, 4, 5)

# generate a sequence of index numbers in reverse order
reverse_seq <- seq(length(my_array), 1, -1)

# use the generated sequence to index the elements of the original array
reversed_array <- my_array[reverse_seq]

# print the reversed array
reversed_array
313 chars
12 lines

Output:

main.r
[1] 5 4 3 2 1
14 chars
2 lines

gistlibby LogSnag