R/lagn.R

#'@title Create lags for matrix
#'
#'@description This function adds lags to a vector.
#'
#'@usage lagn(x,n)
#'
#'@param x A vector to apply the lag to
#'
#'@param n The number of lags to add to the vector
#'
#'@return y A vector with a lag n added
#'
#'
#'@export

lagn <- function(x, n) {
    
    x <- as.matrix(x)
    
    nt <- dim(x)[1]
    
    nc <- dim(x)[2]
    
    if (n > 0) {
        
        x1 <- as.matrix(trimr(x, 0, n))
        
        y <- rbind(matrix(0, n, nc), x1)
    }
    
    if (n < 0) {
        
        x1 <- trimr(x, abs(n), 0)
        
        y <- rbind(x1, matrix(0, abs(n), nc))
    }
    
    
    return(y)
} 

Try the PANICr package in your browser

Any scripts or data that you put into this service are public.

PANICr documentation built on May 2, 2019, 4:40 a.m.