To reverse a vector in R, you can use the built-in rev() function. Here's an example:
main.r
# Create a sequence of numbersx <- 1:5# Reverse the vectory <- rev(x)
# Print the original and reversed vectorsprint(x) # 1 2 3 4 5print(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 numbersx <- 1:5# Reverse the vector using indexingy <- x[length(x):1]
# Print the original and reversed vectorsprint(x) # 1 2 3 4 5print(y) # 5 4 3 2 1