1

I am trying to repeat a for loop in R for a fixed number of times. Here is a sample of the code I want to repeat

for (i in 1:nrow(data)) {
  if (i>130){
    data$deltas[i]=-((-data$p[i]))*data$alphas[i]
  }
  else  {
    data$deltas[i]=
      ((-(((data$p[i+1])-(data$p[i])))*data$alphas[i]) + data$deltas[i+1])
  }
}

I do not have any conditions that need to be met, just need to run the same code over the same dataset n times.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • @akrun using replicate doesn't do the job. The thing is I am trying to populate a variable in a dataset so that the end results is the dataset with the new values. The replicate command does not do that. – confused doughnut Oct 10 '20 at 22:53
  • @akrun I am unable to generate a reproducible example that gives the same output. In my dataset the output keeps changing everytime I run it which is why I wanted to run it a few times. – confused doughnut Oct 10 '20 at 23:03
  • in your code the value of `data$deltas[i]` depends on `data$deltas[i+1]`, which is impossible. (unless `deltas` already exists...).. but it's probably why the value changes every time you run it. – Edo Oct 10 '20 at 23:04
  • just guessing here... is your data 130 rows? is that why there is that if condition inside? – Edo Oct 10 '20 at 23:11
  • if that so, your loop could be simplified to: `library(dplyr);mutate(data, deltas = (p - lead(p, default=0)) * alphas + lead(deltas, default=0))` – Edo Oct 10 '20 at 23:13
  • @Edo yes that was a typo which I fixed and yes it has 130 rows. Thank you for your help but I am still nowhere – confused doughnut Oct 10 '20 at 23:23

1 Answers1

2

If we want to repeat the for loop 'n' times, create a function with the for loop

f1 <- function(data, ival) {  
       
      for(i in head(seq_len(nrow(data)), -1)) {
        if(i > ival) {
         data$deltas[i] <- -((-data$p[i])) * data$alphas[i]
        
        } else  {
         data$deltas[i] <- ((-(((data$p[i+1]) - (data$p[i]))) * 
                            data$alphas[i]) + data$deltas[i+1])
        }
      
      }
    return(data)

  }

and assign the data on each iteration

n <- 10
for(i in seq_len(n)) data <- f1(data, ival = 130)

Using a small reproducible example

set.seed(24)
df1 <- data.frame(deltas = rnorm(10), p = runif(10), alphas = rnorm(10))

-input

df1
#         deltas          p      alphas
#1  -0.545880758 0.09393471 -0.46906069
#2   0.536585304 0.55225375 -0.33498679
#3   0.419623149 0.72516981  1.53625216
#4  -0.583627199 0.13792462  0.60999453
#5   0.847460017 0.22296603  0.51633570
#6   0.266021979 0.68767062 -0.07430856
#7   0.444585270 0.07648914 -0.60515695
#8  -0.466495124 0.59973018 -1.70964518
#9  -0.848370044 0.63014766 -0.26869311
#10  0.002311942 0.04663503 -0.64859151



n <- 10
for(i in seq_len(n)) df1 <- f1(df1, ival = 5)

-output

df1
#         deltas          p      alphas
#1   0.832142549 0.09393471 -0.46906069
#2   0.617163102 0.55225375 -0.33498679
#3   0.559238507 0.72516981  1.53625216
#4  -0.342918176 0.13792462  0.60999453
#5  -0.291043381 0.22296603  0.51633570
#6  -0.051099814 0.68767062 -0.07430856
#7  -0.046287932 0.07648914 -0.60515695
#8  -1.025325821 0.59973018 -1.70964518
#9  -0.169316331 0.63014766 -0.26869311
#10  0.002311942 0.04663503 -0.64859151
akrun
  • 874,273
  • 37
  • 540
  • 662