R中的数据操作:"X"必须是原子的 [英] Data Manipulation in R: 'X' must be atomic
问题描述
我已使用以下命令导入了标题和数字为多列的文件.
irs_data <- read.csv(file="10incyallnoagi.csv")
I have imported a file with headings and numbers in multiple columns using the following command.
irs_data <- read.csv(file="10incyallnoagi.csv")
我想将一列中的值除以另一列,然后确定最高的3个值.
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)
我不断收到错误提示.作为 R 的初学者,在这种情况下,"x必须是原子的"是什么意思.
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
推荐答案
问题是salary_var
是包含单个元素的列表.然后,对sort()
的调用试图对列表进行排序,而不是对原子元素进行排序.通过运行str(salary_var)
,您可以看到salary_var
是列表.如果省略c()
,则最终将得到一个只有一列的数据框,这也带来了同样的问题.
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.
两个简单的解决方案:
要对列表元素中的值进行排序,请使用
To sort the values in the element of the list, use
head(sort(salary_var[[1]], decreasing=TRUE), 3)
,其中[[1]]选择列表的第一个元素并对其中的值进行排序.
where the [[1]] selects the first element of the list and sorts the values within it.
或者,将salary_var
显式创建为数字矢量:
Alternatively, create salary_var
explicitly as a numeric vector instead:
salary_var <- (irs_data[13]/irs_data[12])[[1]]
一个注意事项:在您的帖子中,您在调用sort()
时写了new_var
而不是salary_var
,这可能会使其他读者感到困惑.
One note: in your post, you wrote new_var
instead of salary_var
in your call to sort()
which may confuse other readers.
这篇关于R中的数据操作:"X"必须是原子的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!