How to Fix in R: error in select unused arguments


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

12 Replies to “How to Fix in R: error in select unused arguments”

  1. While it’s a proper solution, this may require altering tons of existing code. Additional option:
    select <- dplyr::select
    If one uses more dplyr than MASS, this will solve the problem. If one needs to call the mass one – call it via MASS::select.

  2. Thank you VERY much for this!
    After a frustrating half an hour of trying to get my R code to work, I found your solution. I would not have figured out the problem without your help. 20 seconds later, my code was working again.

  3. Another solution is to load MASS before dplyr.
    “`
    library(MASS)
    library(dplyr)

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

    Attaching package: ‘dplyr’

    The following object is masked from ‘package:MASS’:

    select

    The following objects are masked from ‘package:stats’:

    filter, lag

    The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

    # A tibble: 3 × 2
    cyl avg_mpg

    1 4 26.7
    2 6 19.7
    3 8 15.1
    “`

Leave a Reply

Your email address will not be published. Required fields are marked *