使用带有嵌套 df 的 map_dbl 无法正确访问数据帧?

Emi*_*ily 2 r purrr

我正在开展一个项目,需要找到在 3 维空间中测量的一堆行为与 3 维空间中预先识别的点之间的距离。我编写了一个函数来计算点与单个行为之间的距离,当我仅将其应用于一种行为时,该函数会起作用。但是,我需要将其应用于更大数据框中的约 750 个行为。因此,我希望按术语嵌套较大的行为数据帧,然后使用 map_dbl 将函数应用于每个嵌套数据帧。但是,我不断收到错误:

\n

错误:mutate()列有问题distance。\n\xe2\x84\xb9 distance = map_dbl(data, calc_distance_from_beh)。\nx 连接列必须存在于数据中。\nx 有问题dim。\n\xe2\x84\xb9 错误发生在第 1 行。

\n

当将 map_dbl 应用于嵌套数据帧时,似乎发生了一些事情,它无法访问要加入的“暗淡”列,我不确定为什么。

\n

我在下面提供了一个可重现的示例,其中只有两种行为。

\n

可重现的例子:

\n
behaviors <- tibble(term = rep(c("abandon", "abet"), each = 3),\n                   estimate = c(-3.31, -0.08, -0.11, 0.03, 0.34, -0.18),\n                   dim = c("E", "P", "A", "E", "P", "A"))\n\noptimal_behavior <- tibble(actor = "civil_engineer",\n                          object = "civil_engineer",\n                          opt_beh = c(1.905645, 0.9960085, -0.17772678),\n                          dim = c("E", "P", "A"))\n\n\ncalc_distance_from_beh <- function(nested_df){\n  \n      optimal_behavior <- as_tibble(optimal_behavior)\n      nested_df <- as_tibble(nested_df)\n      \n      df_for_calculations <- left_join(optimal_behavior, nested_df, by = "dim")\n      \n      df_for_calculations %>% \n            mutate(dist = (estimate-opt_beh)^2) %>% \n            summarise(total_dist = sum(dist)) %>% \n        pull()\n}\n\n\nbehaviors_distance <- behaviors %>% \n                      nest_by(term) %>% \n                      mutate(distance = map_dbl(data, calc_distance_from_beh))\n
Run Code Online (Sandbox Code Playgroud)\n

akr*_*run 5

如果“值”列被命名为estimate,就在ungroup后面nest_by(因为nest_by创建了一个rowwise属性来阻止map访问每个元素)

\n
library(purrr)\nlibrary(dplyr)\nbehaviors %>% \n          nest_by(term) %>% \n          ungroup %>%\n          mutate(distance = map_dbl(data, calc_distance_from_beh))\n# A tibble: 2 \xc3\x97 3\n  term                  data distance\n  <chr>   <list<tibble[,2]>>    <dbl>\n1 abandon            [3 \xc3\x97 2]    28.4 \n2 abet               [3 \xc3\x97 2]     3.95\n
Run Code Online (Sandbox Code Playgroud)\n
\n

或者代替,我们可以直接应用该map函数mutaterowwise

\n
behaviors %>%\n    nest_by(term) %>%\n    mutate(distance = calc_distance_from_beh(data)) %>%\n    ungroup\n
Run Code Online (Sandbox Code Playgroud)\n

-输出

\n
# A tibble: 2 \xc3\x97 3\n  term                  data distance\n  <chr>   <list<tibble[,2]>>    <dbl>\n1 abandon            [3 \xc3\x97 2]    28.4 \n2 abet               [3 \xc3\x97 2]     3.95\n
Run Code Online (Sandbox Code Playgroud)\n