可能重复:
R interpreting variable in data frame as factor; it isn’t
我在使用R编写的一些代码时遇到了问题,我认为我的问题源于对R中因素的理解不足。
我想要做什么:我有一个列号,我想创建另一列bin描述(例如1,2,3等对应bin1,bin2等。 )
以下是代码:
## import data to data frame
rawData = read.table("Data.csv", sep = ",", header = TRUE)
## reduce socre into bins
rawData["s10_BIN"] = NA
for(i in 1:length(rawData[,"foo"]))
{
if(rawData[i,"s10"] > 0 && rawData[i,"s10"] <= .5)
{
rawData[i,"s10_BIN"] = 1
}
else if(rawData[i,"s10"] > .5 && rawData[i,"s10"] <= 1)
{
rawdata[i,"s10_BIN"] = 2
}
else if(rawData[i,"s10"] > 1 && rawData[i,"s10"] <= 1.5)
{
rawData[i,"s10_BIN"] = 3
}
else if(rawData[i,"s10"] > 1.5 && rawData[i,"s10"] <= 2)
{
rawData[i,"s10_BIN_BIN"] = 4
}
else
rawData[i,"s10_BIN"] = 0
}
我一直收到错误
1: In Ops.factor(rawData[i, "s10"], 0) : > not meaningful for factors
2: In Ops.factor(rawData[i, "s10"], 0.5) :
<= not meaningful for factors
有人可以让我了解我所缺少的内容吗?谢谢!
编辑: 以下是数据的前两行:
rawData[1:2,]
ID PRODUCT GROUP MARKET s10 CLAIMS NUM_CLAIMS s10_BIN
1 1 foo A C NULL -5 2 NA
2 2 foo A D 1.064 1 1 NA
我觉得问题可能是NULL值...