I am new to R and a function that creates the first difference at different lag values (see code below).
dt %>%
arrange(GEO, LF.char, NAICS, new_date) %>%
mutate(fd = VALUE -lag(VALUE), FD_percent = fd/lag(VALUE),
fd3 = VALUE -lag(VALUE, 3), FD_percent3 = fd3/lag(VALUE,3))
I am trying to create something like:
dt %>%
arrange(GEO, LF.char, NAICS, new_date) %>%
mutate_FD()
My attempt at writing the function was
mutate_FD <- function(x){
mutate(fd = x.VALUE -lag(x.VALUE), FD_percent = fd/lag(x.VALUE),
fd3 = x.VALUE -lag(x.VALUE, 3), FD_percent3 = fd3/lag(x.VALUE,3))
}
but i get the following error
Error in mutate(fd = x.VALUE - lag(x.VALUE), FD_percent = fd/lag(x.VALUE), : object 'x.VALUE' not found Called from: mutate(fd = x.VALUE - lag(x.VALUE), FD_percent = fd/lag(x.VALUE), fd3 = x.VALUE - lag(x.VALUE, 3), FD_percent3 = fd3/lag(x.VALUE, 3))
Can someone explain to me what I am doing wrong?