Accessing column with df[col] gives: Error 'x&

2019-01-18 02:59发布

问题:

I have a very simple array on which I want to run ROC curve analysis. But first, when i try to force data into Factor type using command table[1]<-factor(table[1]), i get the error

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

Few sample rows from the data are give below

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

Can someone tell me what I am doing wrong ? result of str(table) is given below:

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

回答1:

The function [ applied to a data frame returns a data frame (if only one argument is used). If you want to access a single column and return it as a vector, you have to use [[ instead.

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

But this may not be necessary since both columns are factors (see your str output).

By the way: table is not a good name for an object, since it's also the name of a basic R function.