<分区>
我是 R 的新手,我正在尝试创建一个新列,即一列减去另一列。例如:
price <- c("$10.00", "$7.15", "$8.75", "12.00", "9.20")
quantity <- c(5, 6, 7, 8, 9)
price <- as.factor(price)
quantity <- as.factor(quantity)
df <- data.frame(price, quantity)
在我的实际数据集中,所有列都作为因子导入。当我尝试创建新列时,我得到了这个:
diff <- price - quantity
In Ops.factor(price, quantity): - not meaningful for factors
我尝试使用 as.numeric(df)、as.numeric(levels(df))、as.numeric(levels(df))[df] 将数据强制转换为数字,并将 stringsAsFactors 设置为 false,但是数据被转换为 NA。 Data.matrix 更改值。还有另一种方法可以使上述等式起作用吗?谢谢!