我试图在两个变量之间求和。
如果我有以下数据:
Name Commodity Amount_cmdt
Alex apple 5
Ben orange 10
Chris apple 25
Alex orange 10
Alex apple 10
Chris orange 10
Ben apple 5
我想要一个最终的数据集,像这样:
Name Commodity Amount_cmdt total_apple total_orange
Alex apple 5 15 10
Ben orange 10 5 10
Chris apple 25 25 20
Alex orange 10 15 10
Alex apple 10 15 10
Chris orange 10 25 20
Ben apple 5 5 10
Chris orange 10 25 20
最终,当我有了每个人拥有的苹果和橙子的数量时,我可以丢弃重复的东西。但是,我该如何制定该语句:
如果名称=克里斯,商品=橙色,那么total_orange = sum(Amount_cmdt)?
我写了以下内容,但它是对所有苹果或所有橙子求和,而不管其名称如何:
foreach var of varlist Name {
foreach var of varlist Commodity {
replace total_apple = sum( Amount_cmdt) if Commodity == "apple"
replace total_orange = sum( Amount_cmdt) if Commodity == "orange"
}
}
list
答案 0 :(得分:2)
使用玩具示例:
clear
input strL(name commodity) amount total_apple total_orange
Alex apple 5 15 10
Ben orange 10 5 10
Chris apple 25 25 20
Alex orange 10 15 10
Alex apple 10 15 10
Chris orange 10 25 20
Ben apple 5 5 10
Chris orange 10 25 20
end
以下对我有用:
bysort name commodity: egen totals = total(amount)
bysort name (commodity): generate totalapple = totals[1]
bysort name (commodity): generate totalorange = totals[_N]
list name commodity amount total_apple totalapple total_orange totalorange, abbreviate(15)
+------------------------------------------------------------------------------------+
| name commodity amount total_apple totalapple total_orange totalorange |
|------------------------------------------------------------------------------------|
1. | Alex apple 5 15 15 10 10 |
2. | Alex apple 10 15 15 10 10 |
3. | Alex orange 10 15 15 10 10 |
4. | Ben apple 5 5 5 10 10 |
5. | Ben orange 10 5 5 10 10 |
|------------------------------------------------------------------------------------|
6. | Chris apple 25 25 25 20 20 |
7. | Chris orange 10 25 25 20 20 |
8. | Chris orange 10 25 25 20 20 |
+------------------------------------------------------------------------------------+
编辑:
您可以对以下两种以上的商品进行概括:
clear
input strL(name commodity) amount
Alex apple 5
Ben orange 10
Chris apricot 3
Alex apricot 4
Ben apricot 2
Chris apple 25
Alex orange 10
Alex apple 10
Chris orange 10
Ben apple 5
Chris apricot 15
Alex apricot 6
Chris orange 10
end
bysort name commodity: egen totals = total(amount)
egen commodities = group(commodity)
levelsof commodity, local(allcommodities) clean
local i 0
foreach var of local allcommodities {
local ++i
generate `var' = .
bysort name (commodity): replace `var' = totals if commodities == `i'
bysort name (commodity): egen total`var' = min(`var')
drop `var'
}
drop commodities
修改后的代码段将产生所需的输出:
list name commodity amount total*, abbreviate(15)
+-------------------------------------------------------------------------------+
| name commodity amount totals totalapple totalapricot totalorange |
|-------------------------------------------------------------------------------|
1. | Alex apple 5 15 15 10 10 |
2. | Alex apple 10 15 15 10 10 |
3. | Alex apricot 6 10 15 10 10 |
4. | Alex apricot 4 10 15 10 10 |
5. | Alex orange 10 10 15 10 10 |
|-------------------------------------------------------------------------------|
6. | Ben apple 5 5 5 2 10 |
7. | Ben apricot 2 2 5 2 10 |
8. | Ben orange 10 10 5 2 10 |
9. | Chris apple 25 25 25 18 20 |
10. | Chris apricot 3 18 25 18 20 |
|-------------------------------------------------------------------------------|
11. | Chris apricot 15 18 25 18 20 |
12. | Chris orange 10 20 25 18 20 |
13. | Chris orange 10 20 25 18 20 |
+-------------------------------------------------------------------------------+
答案 1 :(得分:2)
@Pearly Spencer给了您您所要的内容,但是详细的代码确实显示出这是一个非常扭曲的数据结构-我预计使用它会很尴尬。
此外,您无需重复计算,也不必删除重复项,因为您可以直接获得简单的结构。
请注意,这会破坏原始数据集,因此保留原始数据始终是一个好主意。另外,我们无法评论您可能还有的其他变量。
这两种布局中的一种或两种都可能有相同或更多的帮助。
<View pointerEvents="none">
{/* your corner images */}
</View>