【问题标题】:Accessing column with df[col] gives: Error 'x' must be atomic for 'sort.list'使用 df[col] 访问列给出:错误 'x' must be atomic for 'sort.list'
【发布时间】:2013-01-24 07:49:16
【问题描述】:

我有一个非常简单的数组,我想在上面运行 ROC 曲线分析。但首先,当我尝试使用命令table[1]<-factor(table[1]) 将数据强制转换为因子类型时,我得到了错误

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 ...

【问题讨论】:

    标签: r vector indexing syntax dataframe


    【解决方案1】:

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

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

    但这可能不是必需的,因为两列都是因子(请参阅您的 str 输出)。

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

    【讨论】:

    • 我花了很长时间尝试使用as.vector(table[1]) 来获取向量。为什么这不会产生与table[[1]] 相同的输出?
    猜你喜欢
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2012-06-14
    • 2017-04-08
    相关资源
    最近更新 更多