How to create a new variable to indicate change in T compared to T-1

Hi Everyone,

Gvkey <- c(008440, 008440, 008440, 008440, 029901, 029901, 029901, 007331, 007331, 007331 )
Name <- c("A. James Dearlove", "A. James Dearlove", "A. James Dearlove", "A. James Dearlove", "A. Jayson Adair", "A. Jayson Adair", "A. Adams"," A. L. Giannopoulos", "A. Johnson", "A. Johnson")

df <- data.frame(Gvkey, Name)

Currently I am trying to create a new variable that indicates if a CEO change has occured, this code worked last week but not I can't seem to execute the code again.
My code is:

Thesis <- Thesis%>%
group_by(gvkey) %>%
arrange(fyear) %>%
mutate(ceo = ifelse(Thesis$EXEC_FULLNAME == lag(EXEC_FULLNAME),
c(0),
c(1)))

My error is:

Error: Column ceo must be length 14 (the group size) or one, not 157467
In addition: Warning message:
In Thesis$EXEC_FULLNAME == lag(EXEC_FULLNAME) :
longer object length is not a multiple of shorter object length

Does anyone know what I did wrong and knows how to fix it or knows another method to get to my goal?

Thank you in advance!

Try dropping the Thesis$ part from the call to mutate, i.e.,

mutate(ceo = ifelse(EXEC_FULLNAME == lag(EXEC_FULLNAME), 0, 1))
1 Like

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

Thanks a lot!
Your suggestion worked.