• Register
0 votes

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

Please log in or register to answer this question.

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


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
1 answer 384 views
0 votes
1 answer 223 views
0 votes
2 answers 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?
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 605 views
How to convert non-finite calues to integer?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 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.
asked Mar 13 Wafa Abu Yousef 6.1k points
0 votes
1 answer 55 views
Problem: Why I got the error : Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-na) cases.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 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.
asked Aug 12, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 3.5K views
Problem : While I am trying to run the boxcox transformation with the following code urban1 <- subset(ski,urban <= 4,na.rm=TRUE) ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE) urban1 <- as.numeric((as.character(urban1))) a <- (ski$gender*urban1) ... -> boxcar -> boxcar.formula -> lm -> lm.fit Execution halted Please let me know if anyone knows the solution for this error message.
asked Nov 27, 2019 alecxe 7.5k points
1 vote
1 answer 2.7K views
Problem : I want to perform a logistic regression but I am facing following error I am unable to find my mistake. summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles)) Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, ... In Ops.factor(eta, offset) : -' nicht sinnvoll für Faktoren 3: In Ops.factor(y, mu) : -' nicht sinnvoll für Faktoren
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 1.5K views
Problem : Suppose I have data.frame a And I am using following code m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) Here the col2 has some NA values And col3,col4 have values less than 1. I am getting following error: Error in lm.fit ... because of the NAs. Problem with this warning is interpreted by every google results is pointing towards the NA values. Have I misinterpreted the error ?
asked Nov 22, 2019 peterlaw 6.9k points