Search code examples
rdplyrforcats

Unrecognized variable after a pipe with dplyr


I am using the gapminder dataset for making an interactive barplot with plotly library. I want to reorder the levels in continent variable using forcats library but it does not recognize this variable when I put it after the pipe. This is the script I am using:

gapminder %>%
        filter(year==2002) %>%
        mutate(continent=fct_reorder(continent, pop, .desc=T))

I would like to know why the variable "continent" does not pass through the pipe. Thanks.


Solution

  • Try this in order to save your pipe operations on gapminder:

    gapminder %>%
            filter(year==2002) %>%
            mutate(continent=fct_reorder(continent, pop, .desc=T)) -> gapminder