jvalenti
jvalenti

Reputation: 640

R function for creating, naming and lagging variables

I have some data like so:

a <- c(1, 2, 9, 18, 6, 45)
b <- c(12, 3, 34, 89, 108, 44)
c <- c(0.5, 3.3, 2.4, 5, 13,2)

df <- data.frame(a, b,c)

I need to create a function to lag a lot of variables at once for a very large time series analysis with dozens of variables. So i need to lag a lot of variables without typing it all out. In short, I would like to create variables a.lag1, b.lag1 and c.lag1 and be able to add them to the original df specified above. I figure the best way to do so is by creating a custom function, something along the lines of:

lag.fn <- function(x) {
    assign(paste(x, "lag1", sep = "."), lag(x, n = 1L)
    return (assign(paste(x, "lag1", sep = ".")
}

The desired output is:

a.lag1 <- c(NA, 1, 2, 9, 18, 6, 45)
b.lag1 <- c(NA, 12, 3, 34, 89, 108, 44)
c.lag1 <- c(NA, 0.5, 3.3, 2.4, 5, 13, 2)

However, I don't get what I am looking for. Should I change the environment to the global environment? I would like to be able to use cbind to add to orignal df. Thanks.

Upvotes: 0

Views: 100

Answers (4)

r.user.05apr
r.user.05apr

Reputation: 5456

If everything else fails, you can use a simple base R function:

my_lag <- function(x, steps = 1) {
  c(rep(NA, steps), x[1:(length(x) - steps)])
}

Upvotes: 1

akrun
akrun

Reputation: 887911

We can use mutate_all

library(dplyr)
df %>% 
   mutate_all(funs(lag = lag(.)))

Upvotes: 1

G. Grothendieck
G. Grothendieck

Reputation: 270268

The data frame statement in the question is invalid since a, b and c are not the same length. What you can do is create a zoo series. Note that the lag specified in lag.zoo can be a vector of lags as in the second example below.

library(zoo)

z <- merge(a = zoo(a), b = zoo(b), c = zoo(c))
lag(z, -1)  # lag all columns

lag(z, 0:-1)  # each column and its lag

Upvotes: 2

neilfws
neilfws

Reputation: 33802

Easy using dplyr. Don't call data frames df, may cause confusion with the function of the same name. I'm using df1.

library(dplyr)
df1 <- df1 %>%
  mutate(a.lag1 = lag(a),
         b.lag1 = lag(b),
         c.lag1 = lag(c))

Upvotes: 2

Related Questions