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

720 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`.

## Related questions

384 views
223 views
189 views
Problem : I am stuck because of following weired error error in sort.list(y) : 'x' must be atomic for 'sort.list' have you called 'sort' on a list?
605 views
How to convert non-finite calues to integer?
73 views
Problem: This is my problem: Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : na/nan/inf in 'y' Any help.
55 views
Problem: Why I got the error : Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-na) cases.
849 views
Problem: I have only the fundamental knowledge related to R. I want to simulate the very basic moving average process in R which is as shown below for your reference: x_t = Z_(t-1)-0.4Z_(t-2)+Z_t. Please find below my code in R: z = rnorm(500, 0, 1) x ... was unable to find the exact solution for my above error. I am looking for some help from R expert if possible to fix my above R related error.