R Error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic (Example)
In this R tutorial you’ll learn how to handle the “Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic”.
The page looks as follows:
It’s time to dive into the examples.
Example Data
We’ll use the following data as basement for this R programming tutorial.
my_list <- list(3, 2, 1) # Create example list my_list # Print example list # [[1]] # [1] 3 # # [[2]] # [1] 2 # # [[3]] # [1] 1 |
my_list <- list(3, 2, 1) # Create example list my_list # Print example list # [[1]] # [1] 3 # # [[2]] # [1] 2 # # [[3]] # [1] 1
As you can see based on the previous RStudio console output, our example data is a list containing three list elements. Each of these list elements contains a numeric value.
Example 1: Reproduce the Error in sort.int : ‘x’ must be atomic
Example 1 illustrates how to replicate the “Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic” in the R programming language.
Let’s assume that we want to sort the values in our list. Then, we might try to apply the sort function to our list as shown below:
sort(my_list) # Try to sort list # Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : # 'x' must be atomic |
sort(my_list) # Try to sort list # Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : # 'x' must be atomic
Unfortunately, the RStudio console returns the “Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic” after executing the previous R code.
The reason for that is that the list function cannot be applied to list objects.
In this example this might be obvious. However, make sure to check whether you are trying to sort a list object when this error message occurs.
So how can we solve this problem?
Example 2: Fix the Error in sort.int : ‘x’ must be atomic
This example shows how to debug the “Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic”.
One solution for this might be to unlist our list before sorting it. For this, we can use the unlist function as shown below:
sort(unlist(my_list)) # Unlist before sort # [1] 1 2 3 |
sort(unlist(my_list)) # Unlist before sort # [1] 1 2 3
The previous R code has returned a vector containing all list elements in a sorted order.
Video & Further Resources
Do you need further information on the contents of this tutorial? Then I recommend having a look at the following video on my YouTube channel. I illustrate the topics of this tutorial in the video.
The YouTube video will be added soon.
In addition, you might want to read some of the other articles on my website. You can find some articles below:
- Error in barplot.default() : ‘height’ must be a vector or a matrix
- Error in hist.default : ‘x’ must be numeric
- Error : ‘names’ attribute must be the same length as the vector
- Error in as.Date.numeric(X) : ‘origin’ must be supplied
- Handling Warnings & Errors in R (Cheat Sheet)
- R Programming Language
This article has demonstrated how to deal with the “Error in sort.int(x, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic” in R programming. Don’t hesitate to tell me about it in the comments, if you have any further comments and/or questions.