9

I have imported a file with headings and numbers in multiple columns using the following command. irs_data <- read.csv(file="10incyallnoagi.csv")

I would like to divide the values in 1 column by another and then determine the highest 3 values.

     salary_var <- c(irs_data[13]/irs_data[12])
     head(sort(new_var, decreasing=TRUE), 3) 

I keep getting the constant error. As a beginner to R, what does it mean "x must be atomic" in this context.

Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2657817
  • 652
  • 2
  • 9
  • 18
  • Hi, could you provide sample data so that contributors could assist? – KFB Oct 08 '14 at 00:34
  • The data can be found http://www.irs.gov/uac/SOI-Tax-Stats-County-Data, when you click on 2010. The file is 10incyallnoagi.csv. – user2657817 Oct 08 '14 at 00:39
  • 1
    Suggest to provide a snippet of data here. – KFB Oct 08 '14 at 00:44
  • http://snag.gy/YG9iF.jpg – user2657817 Oct 08 '14 at 00:47
  • Try `salary_var <- c(irs_data[[13]]/irs_data[[12]])`. It's not clear if `new_var` should actually be `salary_var` in your sort command. If not, then we need more info. The error is there because `new_var` is a list and lists are not atomic vectors. – Rich Scriven Oct 08 '14 at 00:55
  • You need to either do what @RichardScriven suggested or do `c(irs_data[,13]/irs_data[,12])` (but there's no need for the `c()` in either case). Do a `str(irs_data[13]))` vs a `str(irs_data[,13])` or `str(irs_data[[13]])` to see the difference in type/class. – hrbrmstr Oct 08 '14 at 00:59

2 Answers2

12

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.

Sean Hughes
  • 328
  • 3
  • 7
7

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) 
Sameer Sinha
  • 71
  • 1
  • 2