我有一个数据框,如下所示(Price2
变量除外):
+------+-----------+----------+-------+------+-------------+
| Name | Day | Time | Price | Size | Price2 |
+------+-----------+----------+-------+------+-------------+
| A | 24-Mar-08 | 10:30:01 | 1 | 3 | 0.333333333 |
| A | 24-Mar-08 | 10:30:01 | 4 | 4 | 1.777777778 |
| A | 24-Mar-08 | 10:30:01 | 3 | 2 | 0.666666667 |
| A | 24-Mar-08 | 11:03:12 | 1 | 4 | 0.8 |
| A | 24-Mar-08 | 11:03:12 | 4 | 1 | 0.8 |
| A | 25-Mar-08 | 10:30:01 | 3 | 4 | 2 |
| A | 25-Mar-08 | 10:30:01 | 8 | 2 | 2.666666667 |
| A | 25-Mar-08 | 11:13:59 | 3 | 2 | 0.428571429 |
| A | 25-Mar-08 | 11:13:59 | 2 | 4 | 0.571428571 |
| A | 25-Mar-08 | 11:13:59 | 5 | 5 | 1.785714286 |
| A | 25-Mar-08 | 11:13:59 | 3 | 3 | 0.642857143 |
| A | 25-Mar-08 | 11:59:01 | 1 | 5 | 1 |
| B | 24-Mar-08 | 10:30:01 | 3 | 6 | 2.571429 |
| B | 24-Mar-08 | 10:30:01 | 4 | 1 | 0.571428 |
| B | 24-Mar-08 | 11:30:01 | 3 | 2 | 2 |
| B | 24-Mar-08 | 11:30:01 | 5 | 1 | 1.666667 |
| B | 25-Mar-08 | 11:30:01 | 7 | 3 | 1.909090909 |
| B | 25-Mar-08 | 11:30:01 | 4 | 6 | 2.181818182 |
| B | 25-Mar-08 | 11:30:01 | 2 | 2 | 0.363636364 |
| B | 25-Mar-08 | 12:00:00 | 6 | 2 | 6 |
+------+-----------+----------+-------+------+-------------+
我想在Stata中计算Price2
,Price
乘以Size
并除以每秒Size
的总和。
答案 0 :(得分:1)
首先,为每组Name
- Day
- Time
生成一个大小的总和。然后做其余的数学运算 - 将大小乘以价格并除以大小的总和:
bys Name Day Time: egen sumPrice = total(Size)
gen Price2 = Price * Size / sumPrice
还要检查一组Name="B"
,Day = "24-Mar-08"
,Time = "10:30:01"
。您示例中的Price2
与重新计算的Price2
不匹配。其他值匹配。