reverse a vector in r in r

To reverse a vector in R, you can use the built-in rev() function. Here's an example:

main.r
# Create a sequence of numbers
x <- 1:5

# Reverse the vector
y <- rev(x)

# Print the original and reversed vectors
print(x)  # 1 2 3 4 5
print(y)  # 5 4 3 2 1
161 chars
10 lines

Alternatively, you can reverse a vector by indexing it in reverse order. Here's an example:

main.r
# Create a sequence of numbers
x <- 1:5

# Reverse the vector using indexing
y <- x[length(x):1]

# Print the original and reversed vectors
print(x)  # 1 2 3 4 5
print(y)  # 5 4 3 2 1
184 chars
10 lines

gistlibby LogSnag