我正在尝试创建如下功能:
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]
}
}
但是,当我在数据帧上应用此函数时,出现未知或统一的列错误。请在下面找到一个示例:
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'.
我不确定我是否正确编写了函数。任何帮助将不胜感激