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

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

by Tutor Aspire

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

You may also like