Open In App

How to Fix: could not find function “%>%” in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to fix, could not find function “%>%” in R Programming Language. The pipe operator %>% was introduced to decrease time and to improve the readability and maintainability of code. This error can occur while don’t have loaded or installed the R package.

Method 1: Using magrittr packages

Producing the Error

To reproduce the error message “could not find function “%>%”” in the R. For the example, Here we are using the “%>%” operator to get a sum of sqrt.

R




1:8 %>% sum %>% sqrt


Output:

Error in 1:8 %>% sum %>% sqrt: could not find function "%>%"
Traceback:

How to fix

It can be only fixed using related packages(magrittr)

R




library("magrittr")
1:8 %>% sum %>% sqrt


Output:

6

Method 2: Using dplyr Package

Producing the Error

To reproduce the error message “could not find function “%>%”” in the R. For the example, Here we are using the “%>%” operator to group_by() function followed by summarise() function with an appropriate action to perform.

R




# create data frame
df <- data.frame(Sub = c('Math', 'Math', 'Phy'
                         'Phy', 'Phy', 'Che', 'Che'),
                 Marks = c(8, 2, 4, 9, 9, 7, 1),
                 Add_on = c(3, 1, 9, 4, 7, 8, 2))
  
# Specify data frame
df %>%        
  # Specify group indicator
  group_by(Sub) %>%                      
   
  # Specify column
  summarise_at(vars(Marks),              
               list(name = sum))


Output:

Error in df %>% group_by(Sub) %>% summarise_at(vars(Marks), list(name = sum)): could not find function “%>%”

How to fix

It can be only fixed using related packages(dplyr)

R




# create data frame
df <- data.frame(Sub = c('Math', 'Math', 'Phy'
                         'Phy', 'Phy', 'Che', 'Che'),
                 Marks = c(8, 2, 4, 9, 9, 7, 1),
                 Add_on = c(3, 1, 9, 4, 7, 8, 2))
  
library(dplyr) 
df %>%                                        
  group_by(Sub) %>%                        
  summarise_at(vars(Marks),         
               list(name = sum))


Output:

Sub name
Che 8
Math 10
Phy 22


Last Updated : 28 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads