【发布时间】:2019-12-02 05:09:33
【问题描述】:
我正在阅读 Hadley 的 Advanced R 并进入了这个示例:
boot_permute <- function(df, var) {
n <- nrow(df)
force(var)
function() {
col <- df[[var]]
col[sample(n, replace = TRUE)]
}
}
boot_mtcars1 <- boot_permute(mtcars, "mpg")
head(boot_mtcars1())
#> [1] 16.4 22.8 22.8 22.8 16.4 19.2
head(boot_mtcars1())
#> [1] 17.8 18.7 30.4 30.4 16.4 21.0
谁能解释我为什么他在var 上使用force 但对df 却没有这样做?我知道目的是强制评估变量,但不明白为什么他只为var 这样做。
【问题讨论】:
标签: r lazy-evaluation