Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R具有二进制列的新数据帧_R_Dataframe - Fatal编程技术网

R具有二进制列的新数据帧

R具有二进制列的新数据帧,r,dataframe,R,Dataframe,我正在尝试创建一个新的数据框,它显示促销代码是否在特定日期使用(二进制不是实际的总和\计数),但也显示代码产生的销售额总和 DATA: +---------------+------------+--------------+ | Order Date | Promo Code | Sales Amount | +---------------+------------+--------------+ | 10-29-20 | today20 | 50

我正在尝试创建一个新的数据框,它显示促销代码是否在特定日期使用(二进制不是实际的总和\计数),但也显示代码产生的销售额总和

DATA:
+---------------+------------+--------------+
| Order Date    | Promo Code | Sales Amount | 
+---------------+------------+--------------+
| 10-29-20      |   today20  |   50         |  
+---------------+------------+--------------+
| 10-29-20      |   vip20    |   50         |   
+---------------+------------+--------------+
| 10-29-20      |   today20  |   50         |  
+---------------+------------+--------------+
| 10-28-20      |   vip20    |   50         |   
+---------------+------------+--------------+
| 10-28-20      |   vip20    |   50         |   
+---------------+------------+--------------+
| 10-27-20      |   pc20     |   25         |
+---------------+------------+--------------+
| 10-28-20      |            |   50         |   
+---------------+------------+--------------+
| 10-28-20      |   vip20    |   50         |   
+---------------+------------+--------------+
| 10-27-20      |            |   25         |
+---------------+------------+--------------+
| ....          |      ....  |   ....       |
+---------------+------------+--------------+
| ....          |      ....  |   ....       |
+---------------+------------+--------------+


NEW DATAFRAME
+---------------+------------+--------------+--------------+--------------+
|Order Date     | today20    | vip20        |  pc20        | Sales Total  |
+---------------+------------+--------------+--------------+--------------+
| 10-29-20      |   1        |   1          |    0         |  150.00      |
+---------------+------------+--------------+--------------+--------------+
| 10-28-20      |   0        |   1          |    0         |  100.00      |
+---------------+------------+--------------+--------------+--------------+
| 10-27-20      |   0        |   0          |    1         |   25.00      |
+---------------+------------+--------------+--------------+--------------+
| ....          |      ....  |   ....       | ....         |   ....       |
+---------------+------------+--------------+--------------+--------------+
| ....          |      ....  |   ....       | ....         |   ....       |
+---------------+------------+--------------+--------------+--------------+
使用dcast

我更喜欢使用data.table,所以

library(data.table)

dt=setDT(yourdf)

#This will give you the column names but with the sum in each column.
dt.wide=dcast(data=dt, formula = Order_date ~ Promo_code, fun.aggregate='sum', value.var='Sales_amount')

#Adds sales_total column
dt$Sales_total = apply(dt[,-c('Order_date'),with=F],1,sum)

#Converts promo_code columns to binary 
dt[,lapply(.SD,function(x) x[x>0]=1),.SDcols=unique(yourdf$Promo_code)]

它抛出以下错误:eval中出错(jsub,SDenv,parent.frame()):尝试应用非函数