I am trying to create a function as follows:
LogVolLR <- function(x, y, z) {
for (i in 2:nrow(x)) {
x$y[i] <- (1 - 0.5) * x$y[i-1] + 3 + 8 * x$z[i]
}
}
However I get an unknown or unitialised column error when I apply this function on a data frame. Please find an example below:
library(tidyverse)
set.seed(50)
df <- data_frame(SlNo = 1:2000,
Scenario = rep(c(1, 2, 3, 4),500),
A = round(rnorm(2000, 11, 6)),
B = round(rnorm(2000, 15, 4))) %>%
arrange(Scenario)
df<- df %>% split(f = .$Scenario) %>%
map_dfr(~bind_rows(tibble(Scenario = 0), .x))
df <- df %>% mutate(C = if_else(Scenario != 0, 0, 4),
E = if_else(Scenario != 0, 0, 6))
LogVolLR(df, y = C, z = B)
> LogVolLR(df, y = C, z = B)
There were 50 or more warnings (use warnings() to see the first 50)
Warning messages:
1: Unknown or uninitialised column: 'y'.
2: Unknown or uninitialised column: 'y'.
3: Unknown or uninitialised column: 'z'.
I am not quite sure if I've coded my function correctly. Any help is greatly appreciated