如何修复为R中的列返回na的factor()函数
我正在创建一个基于客户工资5万英镑的培训数据集。然而,我遇到了R中factor()函数的一个问题,它将所有工资值替换为NA如何修复为R中的列返回na的factor()函数,r,R,我正在创建一个基于客户工资5万英镑的培训数据集。然而,我遇到了R中factor()函数的一个问题,它将所有工资值替换为NA newURL1 <- "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" customer <- read.table(newURL1,sep=",",header=FALSE,na.strings="?") names(customer) <- c("I
newURL1 <- "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
customer <- read.table(newURL1,sep=",",header=FALSE,na.strings="?")
names(customer) <- c("ID","workclass","fnlwgt","education","education-num", "marital-Status","occupation","relationship","race","sex","capital-gain","capital-loss","hours-per-week","native-country","wage")
dfCustomer <- customer[-1] #get rid of id column
dfCustomer$wage <- factor(dfCustomer$wage,levels=c(2,4),labels=c("<=50K",">50K"))
运行之后,当您调用factor()
时,它应该在工资下显示“50K”作为值,为什么要提供级别=
?如果您不考虑这一点,您应该可以。级别(dfCustomer$wage)
产生[1]“50K”
(注意前导空格)。当您调用factor()
时分配“为什么要提供级别=
?如果您不使用该选项,您应该可以。级别(dfCustomer$wage)
产生[1]“50K”
(请注意前导空格)。分配时
workclass fnlwgt education education-num marital-Status occupation relationship race sex capital-gain capital-loss hours-per-week native-country wage
1 State-gov 77516 Bachelors 13 Never-married Adm-clerical Not-in-family White Male 2174 0 40 United-States <NA>
2 Self-emp-not-inc 83311 Bachelors 13 Married-civ-spouse Exec-managerial Husband White Male 0 0 13 United-States <NA>
3 Private 215646 HS-grad 9 Divorced Handlers-cleaners Not-in-family White Male 0 0 40 United-States <NA>
4 Private 234721 11th 7 Married-civ-spouse Handlers-cleaners Husband Black Male 0 0 40 United-States <NA>
5 Private 338409 Bachelors 13 Married-civ-spouse Prof-specialty Wife Black Female 0 0 40 Cuba <NA>
6 Private 284582 Masters 14 Married-civ-spouse Exec-managerial Wife White Female 0 0 40 United-States <NA>