在dplyr链中使用table() [英] Using table() in dplyr chain
本文介绍了在dplyr链中使用table()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以解释为什么table()
在dplyr-magrittr管道操作链中不起作用吗?这是一个简单的说明:
Can someone explain why table()
doesn't work inside a chain of dplyr-magrittr piped operations? Here's a simple reprex:
tibble(
type = c("Fast", "Slow", "Fast", "Fast", "Slow"),
colour = c("Blue", "Blue", "Red", "Red", "Red")
) %>% table(.$type, .$colour)
sort.list(y)中的错误:"x."对于"sort.list"必须是原子的 您是否在清单上称呼排序"?
Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?
但这当然有效:
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
推荐答案
此行为是设计使然:由于您自己没有.
,因此仍将小标题作为第一个参数传递,因此它实际上更像是
Since you don't have a .
on it's own, the tibble is still being passed as the first parameter so it's really more like
... %>% table(., .$type, .$colour)
magrittr的官方解决方法是使用花括号
The official magrittr work-around is to use curly braces
... %>% {table(.$type, .$colour)}
这篇关于在dplyr链中使用table()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文