How to Solve R Error: invalid argument to unary operator

by | Programming, R, Tips

This error commonly occurs when the is an unexpected use of a unary operator (+, -, !)

This error can occur if you provide an extra + when inputting a multiline ggplot2 command, for example,

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
   geom_boxplot() +
   + ggtitle("Title")

You can solve this error by removing the extra + symbols.

The error can also occur if you try to order string columns using the minus sign – with order(). You can solve the error by using the decreasing argument in the order() function call.

This tutorial will go through the error in detail and how to solve it with code examples.


Example #1: Multiline ggplot2 command

Consider the following example, we want to use the gapminder dataset to plot life expectancy across all continents. First, we will install and load gapminder

install.packages("gapminder")
library(gapminder)
head(gapminder)
# A tibble: 6 × 6
  country     continent  year lifeExp      pop gdpPercap
  <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
1 Afghanistan Asia       1952    28.8  8425333      779.
2 Afghanistan Asia       1957    30.3  9240934      821.
3 Afghanistan Asia       1962    32.0 10267083      853.
4 Afghanistan Asia       1967    34.0 11537966      836.
5 Afghanistan Asia       1972    36.1 13079460      740.
6 Afghanistan Asia       1977    38.4 14880372      786.

Now that we have the dataset, we will attempt to create the life expectancy boxplots.

ggplot(gapminder, aes(x=continent, y=lifeExp, fill=continent)) +
 geom_boxplot() +
 + ggtitle("Life Expectancy Across Continents")

Let’s run the code to see what happens:

Error in +ggtitle("Life Expectancy Across Continents") :
  invalid argument to unary operator

The error occurs because we have an extra plus-sign + symbol between geom_boxplot() and ggtitle. This typically happens when we copy and paste multiline code. The + sign indicates a new line.

Solution

We can solve this error by removing the extra + symbol. Let’s look at the revised code:

ggplot(gapminder, aes(x=continent, y=lifeExp, fill=continent)) +
   geom_boxplot() +
   ggtitle("Life Expectancy Across Continents")

Let’s run the code to see the result:

Box Plots of Life Expectancy Across Continents gapminder
Box Plots of Life Expectancy Across Continents

We successfully plotted the box plots for life expectancy across the five continents.

Example #2: Sorting Data Frame String Column Descending Order

Let’s look at an example of a data frame with three variables, gender, age, and degree. The data frame describes a sample of degree holders.

population = 10
gender=sample(c("male","female"),population,replace=TRUE)
age = sample(25:75, population, replace=TRUE)
degree = sample(c("MA","MSci","BA","PhD"), population, replace=TRUE)
(final.data = data.frame(gender=gender, age=age, degree=degree))
gender age degree
1  female  59   MSci
2  female  41     MA
3  female  72     MA
4  female  32    PhD
5  female  43     BA
6    male  55   MSci
7    male  43     BA
8  female  43     BA
9  female  49    PhD
10   male  31     BA

If we want to sort by decreasing age, we can use the minus sign inside the order() function call as follows:

final.data[order(-final.data$age),]
 gender age degree
3  female  72     MA
1  female  59   MSci
6    male  55   MSci
9  female  49    PhD
5  female  43     BA
7    male  43     BA
8  female  43     BA
2  female  41     MA
4  female  32    PhD
10   male  31     BA

However, we cannot do this with the degree column because it is type character.

final.data[order(-final.data$degree),]
Error in -final.data$degree : invalid argument to unary operator

In R, we cannot provide a character type object as an argument to the unary operator -.

Solution

We can solve this error by using the decreasing argument in the order function call. Let’s look at the revised code:

final.data[order(final.data$degree, decreasing=TRUE),]

Let’s run the code to see the result:

gender age degree
4  female  32    PhD
9  female  49    PhD
1  female  59   MSci
6    male  55   MSci
2  female  41     MA
3  female  72     MA
5  female  43     BA
7    male  43     BA
8  female  43     BA
10   male  31     BA

We successfully sorted the data frame in descending order of the degree column.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee