I want to write a function to swaphalves a vector of 6 elements,
f2 <- function(x) { if (length(x)<=1) return(x+1) return(x[length(x):1])}
But i want to swap the halves
>Solution :
swap_func <- function(x) {
swapped_vec <- c(which(x > length(x)/2), rev(which(x <= length(x)/2)))
return(swapped_vec)
}
swap_func(x = 1:6)
#[1] 4 5 6 3 2 1
swap_func(x = 1:5)
[1] 3 4 5 2 1