Table of Contents
Fix in R: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic
One error message you may encounter when using R is:
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic
This error occurs when you attempt to sort a list.
By default, R is only capable of sorting atomic objects like vectors. Thus, to use sort() with a list you must first use the unlist() function.
The following example shows how to resolve this error in practice.
How to Reproduce the Error
Suppose we have the following list in R:
#create list some_list <- list(c(4, 3, 7), 2, c(5, 12, 19)) #view list some_list [[1]] [1] 4 3 7 [[2]] [1] 2 [[3]] [1] 5 12 19 #view class class(some_list) [1] "list"
Now suppose we attempt to sort the values in the list:
#attempt to sort the values in the list sort(some_list) Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic
We receive an error because R is not capable of sorting lists directly.
How to Avoid the Error
To avoid the error, we must first use the unlist() function as follows:
#sort values in list sort(unlist(some_list)) [1] 2 3 4 5 7 12 19
Notice that we’re able to successfully sort the list of values without any error because we first used unlist(), which converted the list to a numeric vector.
By default, R sorts the values in ascending order.
However, we can use decreasing=TRUE to sort the values in descending order instead:
#sort values in list in descending order sort(unlist(some_list), decreasing=TRUE) [1] 19 12 7 5 4 3 2
Notice that the values are now sorted in descending order.
Additional Resources
The following tutorials explain how to fix other common errors in R:
Cite this article
stats writer (2024). How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/the-error-x-must-be-atomic-in-the-context-of-using-the-fix-function-in-r-means-that-the-input-variable-x-needs-to-be-a-single-vector-or-scalar-value-rather-than-a-comp/
stats writer. "How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/the-error-x-must-be-atomic-in-the-context-of-using-the-fix-function-in-r-means-that-the-input-variable-x-needs-to-be-a-single-vector-or-scalar-value-rather-than-a-comp/.
stats writer. "How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/the-error-x-must-be-atomic-in-the-context-of-using-the-fix-function-in-r-means-that-the-input-variable-x-needs-to-be-a-single-vector-or-scalar-value-rather-than-a-comp/.
stats writer (2024) 'How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/the-error-x-must-be-atomic-in-the-context-of-using-the-fix-function-in-r-means-that-the-input-variable-x-needs-to-be-a-single-vector-or-scalar-value-rather-than-a-comp/.
[1] stats writer, "How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How to Fix: error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.