. Advertisement .
..3..
. Advertisement .
..4..
The error: could not find function “%>%” in R may appear when you try pipe operation to make your code more readable. It occurs because you have forgotten a crucial step. Read on to find out what it is.
Could not find function “%>%” in R
Reproduce The Error
We are going to process the first 5 rows of the built-in mtcars dataset and use it as an example.
> df <- head(mtcars, 5)
> df
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
The df data frame now contains certain specifications of 5 car models, all extracted from the Motor Trend US magazine.
Let’s say we want to select only models with more than 4 cylinders and then sort them according to their gross horsepower (HP). This is how you normally accomplish this task with the pipe operator (%>%) in R. But as you can see, the R interpreter produces an error instead.
> df %>%
filter(cyl > 4) %>%
arrange(desc(hp))
Error in df %>% filter(cyl > 4) %>% arrange(desc(hp)) :
could not find function "%>%"
Explain The Error
In general, the “could not find function” occurs when you use a non-built-in function or operator that hasn’t been loaded before the call. As a result, R has no way to find the definition of that function or operator.
Most of the time, the simple inclusion of the correct package containing the function you want to use should be enough to get rid of that message.
In R, pipe operators like %>% aren’t built-in in the default implementation. They actually come from third-party packages like magrittr and dplyr. You must import them first before using pipe operators in R.
How To Solve The Error
If you don’t have the magrittr or dplyr package in your R library, you should install them first. From the R command line:
> install.packages("magrittr")
> install.packages("dplyr")
You can now import either of those packages in order to use the %>% operator.
> library(magrittr)
> library(dplyr)
However, remember that in our code, the arrange() function also belongs to the dplyr package. That is why in this case, we need to import it first before running our command:
> library(dplyr)
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
> df %>%
filter(cyl > 4) %>%
arrange(desc(hp))
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
The %>% operator has been called without any problem and helps produce our desired output.
Conclusion
If you see the message: could not find function “%>%” in R, it means you haven’t loaded the magrittr or dplyr package. They are required to provide pipe operations and should be included first.
Leave a comment