I have a dataframe with a set of accounts and their associated usage figures:
account usage Date
1 5 1/1/2015
1 10 1/2/2015
2 8 1/1/2015
2 20 1/2/2015
3 15 1/1/2015
3 12 1/2/2015
I am trying to use dplyr to lag the usage by 1 day,2 days, 3 days, etc...up to 12, and create a column for each lag called lag.usage1, lag.usage2, etc...
I have tried
for(i=2:12){
df<-df%.%group_by(account)%.%mutate(lag.usage[i]=lag(usage,i))}
as well as
for(i in 2:12){
varname <- paste("lag.usage", i , sep="")
df<-df%.%group_by(account)%.%mutate(varname=lag(usage, i))}
Both error out Error: unexpected '='
Can anyone suggest a workaround to resolve this issue using the dplyr framework?