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
The problem is that
salary_var
is a list containing a single-element. The call tosort()
is then trying to sort a list, not an atomic element. You can see thatsalary_var
is a list by runningstr(salary_var)
. If you omit thec()
, 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
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:One note: in your post, you wrote
new_var
instead ofsalary_var
in your call tosort()
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