1

可能重复:
R 将数据框中的变量解释为因子;它不是

我对我用 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

有人可以让我了解我所缺少的吗?谢谢!

编辑:这是数据的前 2 行:

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

4

0 回答 0