有人可以解释为什么table()在dplyr-magrittr管道操作链中不起作用吗?这是一个简单的说明:

tibble(
  type = c("Fast", "Slow", "Fast", "Fast", "Slow"),
  colour = c("Blue", "Blue", "Red", "Red", "Red")
) %>% table(.$type, .$colour)



但这当然有效:
df <- tibble(
  type = c("Fast", "Slow", "Fast", "Fast", "Slow"),
  colour = c("Blue", "Blue", "Red", "Red", "Red")
)

table(df$type, df$colour)


       Blue Red
  Fast    1   2
  Slow    1   1

最佳答案

此行为是设计使然:https://github.com/tidyverse/magrittr/blob/00a1fe3305a4914d7c9714fba78fd5f03f70f51e/README.md#re-using-the-placeholder-for-attributes

由于您自己没有.,因此仍将小标题作为第一个参数传递,因此实际上更像

... %>% table(., .$type, .$colour)

官方的magrittr解决方法是使用花括号
... %>% {table(.$type, .$colour)}

关于r - 在dplyr链中使用table(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44528173/

10-11 02:37