• Register
0 votes
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”.

8 4 2
2,300 points

2 Answers

0 votes

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[[1]], decreasing=TRUE), 3) 

Here a [[1]] 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[13]/irs_data[12])[[1]]
5 2 1
4,980 points
0 votes

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[[1]], decreasing=TRUE), 3)

where the [[1]] 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[13]/irs_data[12])[[1]]

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.

10 6 4
31,120 points

Related questions

0 votes
2 answers 116 views
116 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?
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 38 views
38 views
How to convert non-finite calues to integer?
asked Sep 22 Daniel Anderson 4k points
0 votes
1 answer 153 views
153 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.
asked Aug 12 Raphael Pacheco 4.9k points