【发布时间】:2021-07-23 04:52:27
【问题描述】:
我有一个这样的数据框:
df <- tibble(
i = rep(1:10, times = 5),
t = rep(1:5, each = 10)
) %>%
mutate(y = rnorm(50))
我想应用一个函数,将每个 t 的数据框作为参数:
f <- function(df){
return(lm(y ~ +1, data = df))
}
当我使用管道运算符为嵌套数据框应用 purrr::map 时,出现错误。
# does not work
df_nested <- df %>%
nest(data = c(t, y)) %>%
rename(data_col = data)
df_nested %>%
purrr::map(.x = .$data_col, .f = f)
另一方面,当我不使用管道运算符时,我得到了想要的结果。
# Ok
purrr::map(.x = df_nested$data_col, .f = f)
据我了解,两个代码都应该返回相同的结果。管道运算符的代码有什么问题?
【问题讨论】: