Showing results for 
Search instead for 
Did you mean: 

Alteryx designer Discussions

Find answers, ask questions, and share expertise about Alteryx Designer.
Community v19.9

The latest release includes several enhancements designed to improve your Community experience!

Learn More

Lag function in Time series using R tool in Alteryx


I am trying to use 'Lag' function in R editor in Alteryx.  I have copied my R code from R studio to R tool in Alteryx. After I create the first time-series using TS function, I want another series to be a lag series of the previous one.  I tried 'lag' function from 'stats' package and 'quantmode' package in Alteryx but none of them worked. When I see the data, the second series is not lagged, it is on the same level as the first one. Has anyone tried using LAG in R tool of Alteryx? Please suggest. 


@sanketsaxenaUnfortunately, at this time we do not provide lag or lead functions within the scope of the R or Time Series tools as our Predictive Tools installation does not include any packages/modules with these functions. In theory you could install some custom packages/modules to enable these functions, but this would be outside of the scope of support I can provide. If you would like to see these functions added in the future I would suggest you create a post in the Product Ideas section of the community.




Hi Kevin,


Thank you for your response.  Your answered confused me a little. If you can please clarify it. 

At one place you mentioned, "with in the scope of R". Does that mean the R tool? If so, then how would a custom package installation work if R tool itself does not support it? Please clarify


Second, you suggested installing custom package/module. There are 2 packages I am aware of that contains 'Lag' function. One is stats that comes with base R and Install R package tool gives a message that it should not be updated. Another is 'quantmod'  which I did install using Install R package tool and it got installed but yet did not work.  So, when you said, "install some custom packages/modules to enable these functions", do you mean some other way to install which I am not doing?


Looking forward to your reply. 



@sanketsaxena My apologies. I didn't intend to cause any confusion. The statement regarding this functionality not being available in the scope of the R Tool or Time Series tools'was because technically it is possible to reproduce the behavior of lag and lead functions outside of the predictive tools using a multi-row formula tool. However, I didn't want to include this information initially as it still isn't technically a lag or lead function, and it is likely not suitable to your needs. I am also not sure that the lag and lead functionality being reproduced is the same type of functionality available via these functions in R. If you would like to review this method yourself you can find information on it in the following post:


Regarding installing additional R packages there is a short knowledge article about this here on the Community. It discusses how to install additional packages we don't include with the Predictive Tools, and even links to an app we created that you can download and run to install additional packages.


If you already used this method to install a package that contains these functions you should be able to use them. You do need to make sure you are including that package using a library() call so that package and its functions are available to your R code. Just keep in mind that since these functions/packages aren't natively available in our Predictive Tools that it may or may not work as expected and that we can't provide support for it.


Hi Kevin,


Thank you for your detailed post.  I already had tried all the things that you suggested. However, today I figured out the problem with lag. The problem is that when Alteryx reads the CSV file into the data frame in the R tool, it treats the values as a factor, not as a numeric. I converted the values manually 'factor->character->numeric ' and the lag function with quantmod package worked. 


If anyone in future comes across the same problem, hope this solution will help him/her. 


Thank you for your time and efforts.








@sanketsaxena I am glad you found a solution that works for you. I also want to thank you for posting your findings and solution. I am sure this will be of assistance to other users in the future.