【发布时间】:2023-04-02 21:18:01
【问题描述】:
我有一个包含 817.741 笔交易和 12 个变量的杂货购物数据集,如下所示:
Date Customer_ID Age_Group Address Product_Subclass Product_ID Quantity Asset Price Price_Per_Unit Profit_Per_Item Budget_Item
1: 2000-11-01 00:00:00 46855 D E 110411 4.710085e+12 3 51 57 19 6 FALSE
2: 2000-11-01 00:00:00 539166 E E 130315 4.714981e+12 2 56 48 24 -8 TRUE
3: 2000-11-01 00:00:00 663373 F E 110217 4.710266e+12 1 180 135 135 -45 TRUE
我已通过以下方式初始化变量“预算项目”:
Total_Input[,"Budget_Item"] <- FALSE
现在我希望预算项目为“真”以防万一(价格 - 资产
当前代码for循环:
for(i in 1:nrow(Total_Input)){
if(Total_Input$Price[i] - Total_Input$Asset[i] <0){Total_Input$Budget_Item[i] = TRUE}
}
【问题讨论】:
-
Total_Input$Budget_Item <- (Total_Input$Price - Total_Input$Asset) < 0 -
@akrun,是的,我的数据集是 data.table,已确认。
标签: r for-loop data.table