6.9 C
London
Thursday, December 19, 2024
HomeRFix Common Errors in RHow to Fix in R: argument “no” is missing, with no default

How to Fix in R: argument “no” is missing, with no default

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 ifelse(df$team == "B", "Boston") : 
  argument "no" is missing, with no default

This error occurs when you use the ifelse() function in R but forget to include a third argument to specify the value that should be returned if the logical test returns false.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following data frame in R:

#create data frame
df frame(team=c('B', 'B', 'B', 'B', 'C', 'C', 'C', 'D'),
                 points=c(12, 22, 35, 34, 20, 28, 30, 18),
                 assists=c(4, 10, 11, 12, 12, 8, 6, 10))

#view data frame
df

  team points assists
1    B     12       4
2    B     22      10
3    B     35      11
4    B     34      12
5    C     20      12
6    C     28       8
7    C     30       6
8    D     18      10

Now suppose we attempt to use the ifelse() function to create a new column called city that contains “Boston” if the value in the team column is equal to “B”:

#attempt to create new column with team city
df$city =='B', 'Boston')

Error in ifelse(df$team == "B", "Boston") : 
  argument "no" is missing, with no default

We receive an error because we failed to provide a third argument in the ifelse() function that specifies the value we should return if the value in the team column is not equal to “B”.

How to Fix the Error

The way to fix this error is to simply provide a third argument in the ifelse() function that specifies the value we should return if the value in the team column is not equal to “B”.

The following syntax shows how to do so:

#create new column with team city
df$city =='B', 'Boston', 'Other')

#view updated data frame
df

  team points assists   city
1    B     12       4 Boston
2    B     22      10 Boston
3    B     35      11 Boston
4    B     34      12 Boston
5    C     20      12  Other
6    C     28       8  Other
7    C     30       6  Other
8    D     18      10  Other

Notice that we don’t receive  any error this time since we provided a third argument to the ifelse() function.

In this example, the ifelse() function returns a value of “Boston” if the value in the team column is equal to “B” or a value of “Other” if the value in the team column is anything else.

Additional Resources

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

How to Fix in R: NAs Introduced by Coercion
How to Fix in R: Subscript out of bounds
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: number of items to replace is not a multiple of replacement length

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