Error in sort.list(y) : ‘x’ must be atomic for ‘sort.list’

Simple English Explanation

While the initial presentation of this error can be rather intimidating, the root cause is straightforward. In fact, it is basically a variant of another atomic vector related error we describe here.

To quote “The Bard”, the problem, Dear Brutus, is not in our stars but rather in our data structures. Specifically the data structures we are trying to sort or – in the related example – transform.

R has six atomic data structures (list of options here) which are generally referred to as vectors. These are locked down in terms of what they can contain and immutable. Think array, the C++ variety.

Lists in R, on the other hand, are far more flexible. For those C++ programmers out there, think linked list. Lists have elements, which can be attached to other elements and can contain any type of R object. Including, for that matter, other lists. Which in turn can contain other lists. And so on and so forth. In any event – think flexible. The whole entity is held together by the list structure, which is designed to be dynamically allocated (attach and remove list elements) and flexible with regards to its content.

Sorting – is not flexible. We’re taking a list of specific values and ranking them. I can rank numbers. I can rank letters. I can probably even rank logical variables. I cannot, however, rank lists. We need to reduce this down to something that R can easily look at and rank into the proper order.

This generally means unpacking the list using the “unlist” function and running the results through sort.

Fixing ‘x’ must be atomic for ‘sort.list’

A little spelunking in your code is likely in order. The essence of the issue is you’re feeding a non-atomic data type such as a list into a method which expects something a little simpler, ideally a vector.

Find the broken part of your process and use unlist() to unpack your information into a usable form.