11.1 C
London
Sunday, July 7, 2024
HomeRFix Common Errors in RHow to Fix in R: error in select unused arguments

How to Fix in R: error in select unused arguments

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories