How to Solve R Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic

by | Programming, R, Tips

This error occurs when you try to sort a list without first using the unlist() function.

To solve this error, you must use the unlist() function before calling sort(), for example:

sort(unlist(a_list))

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


The Sort function in R

The sort function sorts a vector a factor into ascending or descending order. sort expects a class, or a numeric, complex, character or logical vector. sort.int expects a numeric, complex, character, or logical vector or a factor. If you try to pass a list to sort instead of a vector or a factor you will raise the error sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’.

Example: sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic

Let’s look at an example of a list of numbers.

numbers <- list(10, 7, 8, 1, 20, 4, 8)
sort(numbers)

We can verify that the numbers object is a list using the class() function.

class(numbers)
[1] "list"

We will attempt to sort the list in ascending order using the sort() function.

sort(numbers)

Let’s run the code to see the result:

Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic

The error occurs because the sort function is only capable of sorting atomic objects like vectors.

Solution

We can solve the error by using the unlist() function to convert the list to a vector. We can verify the unlist() function call returns an atomic vector using the is.atomic and is.vector functions:

numbers <- list(10, 7, 8, 1, 20, 4, 8)
vec <- unlist(numbers)
is.atomic(vec)
is.vector(vec)
[1] TRUE
[1] TRUE

Now that we have an atomic vector we can pass it to the sort function:

sort(vec)

Let’s run the code to see the result:

[1]  1  4  7  8  8 10 20

We can put the above solution all in one line as follows:

sort(unlist(numbers))
[1]  1  4  7  8  8 10 20

If we want to sort in descending order we can pass decreasing=TRUE as the second argument of the sort function call:

sort(unlist(numbers), decreasing=TRUE)

Let’s run the code to get the result:

[1] 20 10  8  8  7  4  1

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 ✨