RobertMc RobertMc -4 years ago 78
R Question

Using table() in dplyr chain

Can someone explain why

doesn't work inside a chain of dplyr-magrittr piped operations? Here's a simple reprex:

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

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

But this works of course:

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

Answer Source

This behavior is by design:

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)

The official magrittr work-around is to use curly braces

... %>% {table(.$type, .$colour)}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download