Problem
What do I do when %>% doesn’t work?
Context
I love the %>% pipe. Originally from magrittr, it’s now characteristic of tidy code. Using %>% has revolutionized how I write code in R (pssst! coming soon: an interactive pipe tutorial!). But sometimes the basic pipe falls short.
table() is one of my favorite functions for exploring data in R: it creates a frequency table of values in a vector. I use table() to do sanity checks on my data, make sure that all factor levels are present, and generally get a sense of how my observations are distributed.
A while back, though, I noticed that table() didn’t play nice with the %>% pipe.
I’ve collected some data on my friends’ pets. Here it is (using pseudonyms, in case anyone has a secret pet they don’t want the world to know about…).

# Load magrittr > library(magrittr) # Create data > pets <- data.frame(friend = c("Mark", "Mark", "Kyle", "Kyle", "Miranda", "Kayla", "Kayla", "Kayla", "Adriana", "Adriana", "Alex", "Randy", "Nancy"), pet = c("cat", "cat", "cat", "cat", "cat", "dog", "cat", "lizard", "cat", "cat", "dog", "dog", "woodpecker"), main_pet_color = c("brown", "brown", "multi", "multi", "brown", "brown", "brown", "orange", "black", "white", "multi", "white", "multi")) # Look at the data > pets friend pet main_pet_color 1 Mark cat brown 2 Mark cat brown 3 Kyle cat multi 4 Kyle cat multi 5 Miranda cat brown 6 Kayla dog brown 7 Kayla cat brown 8 Kayla lizard orange 9 Adriana cat black 10 Adriana cat white 11 Alex dog multi 12 Randy dog white 13 Nancy woodpecker multi
Unsurprisingly, it looks like there are a lot of cats and dogs! There are also a lot of brown pets and a lot of multicolored ones. Let’s say I want to see a frequency table of the pet colors. I know that I can do this with table(), like so:
# Make a frequency table of pet colors
> table(pets$main_pet_color)
black brown multi orange white
1 5 4 1 2
But if I want to use tidy syntax, I’d try to do it this way instead:
# Make a frequency table of pet colors > pets %>% table(main_pet_color) Error in table(., main_pet_color) : object 'main_pet_color' not found
What’s up with this? The syntax should work. pet is definitely a valid variable name in the data frame pets, and if I had used a different function, like arrange(), I would have had no problems:
# Arrange the data frame by pet color
> pets %>% arrange(main_pet_color)
friend pet main_pet_color
1 Adriana cat black
2 Mark cat brown
3 Mark cat brown
4 Miranda cat brown
5 Kayla dog brown
6 Kayla cat brown
7 Kyle cat multi
8 Kyle cat multi
9 Alex dog multi
10 Nancy woodpecker multi
11 Kayla lizard orange
12 Adriana cat white
13 Randy dog white
So why doesn’t this work with table()?? This problem has driven me crazy on several occasions. I always ended up reverting back to the table(pets$main_pet_color) syntax, but I was not happy about it.
Turns out, there’s a simple fix.












