Created
November 28, 2022 19:27
-
-
Save vankesteren/be6d9b951bf9868a458f8a463cdad221 to your computer and use it in GitHub Desktop.
Ring shift right function in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Function to shift the elements of a vector | |
#' | |
#' Shifting elements to the right by `shift`. Use | |
#' negative integers to shift to the left. | |
#' Elements on the right of the vector will enter | |
#' on the left of the vector (ring). | |
#' | |
#' @param x vector to shift | |
#' @param shift how many elements to shift by | |
#' | |
#' @return the vector `x`, elements shifted by `shift` | |
ring_shift_right <- function(x, shift = 1) { | |
P <- length(x) | |
x[(1:P + P - 1 - shift) %% P + 1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Functionality to return a matrix with indicators