Home » How to Fix in R: error in select unused arguments

How to Fix in R: error in select unused arguments

by Erma Khan

One error you may encounter in R is:

Error in select(., cyl, mpg) : unused arguments (cyl, mpg) 

This error occurs when you attempt to use the select() function from the dplyr package in R but also have the MASS package loaded.

When this occurs, R attempts to use the select() function from the MASS package instead and an error is produced.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we attempt to run the following code to summarize a variable in the mtcars dataset in R:

library(dplyr)
library(MASS)

#find average mpg grouped by 'cyl'
mtcars %>%
  select(cyl, mpg) %>%
  group_by(cyl) %>%
  summarize(avg_mpg = mean(mpg))

Error in select(., cyl, mpg) : unused arguments (cyl, mpg)

An error occurs because the select() function from the MASS package clashes with the select() function from the dplyr package.

How to Fix the Error

The easiest way to fix this error is to explicitly tell R to use the select() function from the dplyr package by using the following code:

library(dplyr)
library(MASS)

#find average mpg grouped by 'cyl'
mtcars %>%
  dplyr::select(cyl, mpg) %>%
  group_by(cyl) %>%
  summarize(avg_mpg = mean(mpg))

# A tibble: 3 x 2
    cyl avg_mpg
1     4    26.7
2     6    19.7
3     8    15.1

The code successfully runs because dplyr::select explicitly tells R to use the select() function from the dplyr package instead of the MASS package.

Additional Resources

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: contrasts can be applied only to factors with 2 or more levels

Related Posts