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:
How to Fix in R: Arguments imply differing number of rows
How to Fix in R: error in select unused arguments
How to Fix in R: non-conformable arguments
How to Fix in R: replacement has length zero