Home > other >  Nested for loops using row lags and multipliers defined in loop
Nested for loops using row lags and multipliers defined in loop

Time:09-10

End goal is to take a dataframe and create a new column based on multiplication and addition of prior rows, i.e. if my multipliers are 0.1, 0.2, and 0.3, my addition is z [lag(z) * 0.1] ,then I want to take column Z and transform it 3 times as such (skipping the first row):

z <- 1:4*10
df <- data.frame(z)
Z Z_0.1 Z_0.2 Z_0.3
10 10 10 10
20 21 22 23
30 32.1 34.4 36.9
40 43.21 46.88 51.07

I have been able to get the correct values by manually feeding in the rate and overwriting the existing column:

for (i in 1:nrow(df)) {
    if (i ==1)
      df[i,1] <- df[i,1]
    else
      df[i,1] <- df[i,1]   (df[i-1,1] * 0.1) 
  }

Separately, I can also create column placeholders for the new values:

for (i in seq(0.1, 0.3, by = 0.1)) {
  cola <- paste('col', i, sep = "_")
  df[[cola]] <- 0
}

However, I cannot seem to combine these loops and get the outcome in the above sample table. I have tried this:

for (i in 1:nrow(df2)) {
  for (j in seq(0.1, 0.3, by = 0.1)) {
    cola <- paste('col', j, sep = "_")
    df[[cola]] <- 0
    if (i ==1)
      df[[cola]] <- df[i,1]
    else
      df[[cola]] <- df[i,1]   (df[i-1,1] * j) 
  }
}

But it fills all the new columns with the same values for the whole column

Z Z_0.1 Z_0.2 Z_0.3
10 77.02 81.85 86.68
20 77.02 81.85 86.68
30 77.02 81.85 86.68
40 77.02 81.85 86.68

Appreciate any suggestions. I'm not married to for loops if anyone has an alternative suggestion.

CodePudding user response:

Like this maybe?

Z <- 1:4*10
y <- seq(0.1, 0.3, by = 0.1)
df <- data.frame(Z)

for (i in 1:(length(Z)-1) 1){
  for (j in seq_along(y)){
    df[1,paste0('Z_', y[j])] = Z[1]
    df[i, paste0('Z_', y[j])] = Z[i] (df[i-1, paste0('Z_', y[j])]*y[j])
  }
}
df
#>    Z Z_0.1 Z_0.2 Z_0.3
#> 1 10 10.00 10.00 10.00
#> 2 20 21.00 22.00 23.00
#> 3 30 32.10 34.40 36.90
#> 4 40 43.21 46.88 51.07

Created on 2022-09-09 by the reprex package (v2.0.1)

  • Related