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

230 views

## Problem :

I am trying to do data manipulation using R. I am trying to import the file which is having headings and also numbers in the multiple columns.

I am trying to divide my values in the one column by the other column and then trying to determine the highest values.

But I I keep getting the constant error from R. I am the very beginner to the R, How can I fix the below error

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

## Solution :

I have read your problem, I can understand that you are learning the new technology so you are experiencing the errors while executing your code.

I think the problem is that your list may be containing only one element. And your call to `sort()` is now trying to sort the list and not the single element.

I can suggest you two simple solutions:

To sort your values in a element of your list, you can use

`head(sort(var_sal[], decreasing=TRUE), 3) `

Here a [] is selecting the very first element of your list and then sorting your values within it.

Or you can create var_sal explicitly as the numeric vector instead:

`var_sal <- (irs_data/irs_data)[]`

## Solution:

The problem is that `salary_var` is a list containing a single-element. The call to `sort()` is then trying to sort a list, not an atomic element. You can see that `salary_var` is a list by running `str(salary_var)`. If you omit the `c()`, you'll instead end up with a data frame with a single column, which gives the same problem.

### Two simple solutions:

To sort the values in the element of the list, use
``head(sort(salary_var[], decreasing=TRUE), 3)``

where the [] selects the first element of the list and sorts the values within it.

### Alternatively, create `salary_var` explicitly as a numeric vector instead:

``salary_var <- (irs_data/irs_data)[]``

One note: in your post, you wrote `new_var` instead of `salary_var` in your call to `sort()` which may confuse other readers.

you can use the unlist() to convert the list to a vector as the sort() function takes vector form for sorting. so just use

``head(sort(unlist(new_var), decreasing=TRUE), 3)``

From the output of `str(cc2)`, the variable inside of the data.table, V1, is itself a list. This means that cc2 is a nested list of length 1. The error is occurring because `table` calls `sort.list`, which requires an atomic vector as input.

### Try using `unlist`:

``cc3 <- as.data.frame(table(unlist(cc2)))``

`unlist` will (recursively) extract elements from their list containers. So `unlist(cc2)` will return a vector, which works directly with `table`.

12 views