r - 使用df [col]访问列给出:错误 'x'对于 'sort.list'必须是原子的

标签 r vector indexing syntax dataframe

我有一个非常简单的数组,可以在其上运行ROC曲线分析。但是首先,当我尝试使用table[1]<-factor(table[1])命令将数据强制转换为Factor类型时,出现错误

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

数据中的几行示例如下
GRADE   TRUE-GRADE

benign  Benign
benign  Benign
benign  Benign
benign  Benign
benign  Benign
benign  Benign
benign  Malignant
benign  Malignant
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Benign
indeterminate   Malignant
indeterminate   Malignant
indeterminate   Malignant
likely benign   Benign
likely benign   Benign
likely benign   Benign

有人可以告诉我我在做什么错吗? str(table)的结果如下:
data.frame':   127 obs. of  2 variables:
 $ GRADE        : Factor w/ 5 levels "benign","indeterminate",..: 1 1 1 1 1 1 1 1 1 

 $ BENIGN.MALIGN: Factor w/ 2 levels "Benign","Malignant": 1 1 1 1 1 1 1 1 1 1 ...

最佳答案

应用于数据帧的函数[返回一个数据帧(如果仅使用一个参数)。如果要访问单个列并将其作为 vector 返回,则必须改用[[

table[1] <- factor(table[[1]])

但这不是必需的,因为这两列都是因素(请参见str输出)。

顺便说一句:table并不是对象的好名字,因为它也是基本R函数的名称。

关于r - 使用df [col]访问列给出:错误 'x'对于 'sort.list'必须是原子的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774382/

相关文章:

r - 如何沿非NA元素创建向量序列

c++ - 被 vector<unique_ptr> 击败

SQL - 将多列合并为一列以便于选择和索引

python - 对 Pandas DataFrames 使用逻辑或 bool 索引的正确语法是什么?

r - 在聚合时按最近的前一个日期合并两个数据帧

r - 有条件地将值从一列粘贴到R中的另一列

python - 计算两个 3D 向量之间的角度

r - R中整数向量的大小

r - 将 R 对象传递给 Rust 程序需要哪些步骤?

python - 重新索引错误没有意义