One error you may encounter in R is:
Error: could not find function "%>%"
This error often occurs when you attempt to use the “%>%” function in R without first loading the dplyr package.
To fix this error, you simply need to load the dplyr package first:
library(dplyr)
The following example shows how to fix this error in practice.
How to Reproduce the Error
Suppose we have the following data frame in R that displays the points scored by various basketball players on different teams:
#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
points=c(6, 14, 15, 19, 22, 25, 39, 34))
#view data frame
df
team points
1 A 6
2 A 14
3 A 15
4 A 19
5 B 22
6 B 25
7 B 39
8 B 34
Now suppose we attempt to use the “%>%” function to find the average points scored by players on each team:
#find average points scored by players on each team
df %>%
group_by(team) %>%
summarize(avg_points = mean(points))
We receive an error because we never loaded the dplyr package.
How to Fix the Error
The way to fix this error is to simply load the dplyr package before using the “%>%” function:
library(dplyr)
#find average points scored by players on each team
df %>%
group_by(team) %>%
summarize(avg_points = mean(points))
# A tibble: 2 x 2
team avg_points
1 A 13.5
2 B 30
The output displays the average points scored by players on each team and we receive no error because we loaded the dplyr package before using the “%>%” function.
Additional Resources
The following tutorials explain how to fix other common errors in R:
How to Fix in R: could not find function “ggplot”
How to Fix in R: error in select unused arguments
How to Fix in R: names do not match previous names