r - 在 R 中使用“NA”值在测试数据集中创建目标变量列
问题描述
我想结合测试和训练数据集。在此之前我必须添加一个新列来测试数据集以匹配训练数据集的列数。
我正在使用“NA”值在测试数据集中创建一个新列,这是我使用的代码:
test[,Item_Outlet_Sales := "NA"]
编译这段代码给了我这个错误:
Error in `:=`(Item_Outlet_Sales, "NA") :
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
解决方案
您只需要将数据框转换为 data.table。
x[, X := NA]
Error in `:=`(X, NA) :
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
z <- as.data.table(x)
z[, X := NA]
然后您可以看到添加了名为“X”的列。
在你的情况下,你只需要
test <- as.data.table(test)
test[,Item_Outlet_Sales := "NA"]
并使用 := 表示法添加新列。
推荐阅读
- python - Pyspark DataFrame中基于两列差异的重复行动态次数
- swift - 居中对齐stackView,同时保持固定间距
- javascript - 当我清除选项时,选择 onChange 不再起作用
- qt - Qt/QML:std::vector
处理 - javascript - 如何通过 id 单击单选按钮来激活事件?
- marklogic - MarkLogic - 通过 API 返回数据
- azure - Azure Data Lake Storage x Azure Blob 存储和 Azure 文件存储之间的区别
- python-3.x - 如何使用 while_loop 实现 TensorBoard v2 (tf.contrib.summary)?
- c - 是否有“分支”字符串格式描述符?
- reactjs - 用酶测试材料-ui文本字段中的按键