0

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?

IRTFM
  • 258,963
  • 21
  • 364
  • 487
user2891099
  • 125
  • 1
  • 9
  • Maybe if you put in in parentheses, like `mutate((varname)=lag(usage,i))`? – Frank May 05 '15 at 17:20
  • Oh, it looks like this is the same question: http://stackoverflow.com/questions/26003574/r-dplyr-mutate-use-dynamic-variable-names – Frank May 05 '15 at 17:24

0 Answers0