Error when creating lead variables - FUN=lead

I am trying to create lead variables: this function is not running anymore (it did in the past).
I do not see the error; using lag works fine...but lead does not run through.

sample_did$lead.x1 = ave(sample_did$x1 , sample_did$group_id, FUN=lead)

Error in UseMethod("lead") :
no applicable method for 'lead' applied to an object of class "c('double', 'numeric')"

are you using lead from dplyr, or another library ?
This works...

library(dplyr) # for lead function
iris$lead_petallength = ave(iris$Petal.Length , iris$Species, FUN=lead)

I am using dplyr....maybe other packages are in the way?

you can run conflicts() and also getAnywhere(lead)

Are there other ways to get lead and lag variables for panel data variables?

dplyr::lead would be my preferred way. I'm unaware of others, but they may exist.
I would use a more pure dplyr flow probably

iris %>% 
group_by(Species) %>%
mutate(lead_petal_length = lead(Petal.Length))

I tried the following:

sample_did$x1 = lag(sample_did$x1, n = 1L, default = NA, order_by = sample_did$bank_id)

Error in attr(x, "tsp") <- c(1, NROW(x), 1) :
attempt to set an attribute on NULL

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.