0

I am running the Automatic Variance Ratio (AVR) test on my dataset in R. My Dataset Contains 6 Indices i.e. columns exculing the date column. In this test, I need to use FOR LOOP which would constantly roll over the first column i.e. Date column, and keep changing/moving from the 2nd till the 6th column. I am new to R, therefore, I don't know exactly what to do and how to do it. Currently, I have a code that can run this for only the 2nd column but from the 2nd column onwards it can loop over. All of you are requested to please help me in this regard.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    Here's how to post an R question in a way that people can help you... https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Bill O'Brien May 11 '21 at 18:27

1 Answers1

0

A standard way to loop through the columns of a dataframe is with lapply. If your dataframe is df with 7 columns and you want to loop through columns 2 through 7 and your function is Av.VR() then

output_list <- lapply(df[,2:7], function(x) Av.VR(x))

should yield a list of outputs for each column.

  • Note I have no experience using the function Av.VR().
SteveM
  • 2,226
  • 3
  • 12
  • 16
  • Thanks for your response. That does not work on Matrix. As my dataset is in Matrix form ( for the Auto.VR does not work on list etc). Secondly Av.VR(x) function is not found in the R when I use this function. If you could please simply guide me on how to use FOR LOOP while keeping the first column i.e. the date constant and keep moving or changing the other columns ( from 2nd till 7th columns) in the dataset Please – ناصر رحمان May 11 '21 at 19:44
  • You have to install the vrtest package that contains the Av.VR function first `install.packages('vrtest')`. Then load the vrtest package. `library(vrtest)` To execute the `lapply` function first convert your matrix to a dataframe: `df <- as.dataframe(mymatrix)` – SteveM May 11 '21 at 19:56
  • yes, "vrtest" is installed. If i convert my dataset to dataframe then the AVR test won't work over that as it only work on matrices or vectors. Sorry for making it more and more complicated. – ناصر رحمان May 11 '21 at 20:10
  • It should work because lapply considers a dataframe a list of vectors. Did you actually try it? If it does not work, then I guess it's not the right solution. – SteveM May 11 '21 at 20:33