【发布时间】:2021-07-20 01:37:34
【问题描述】:
我正在尝试根据比例计算两种商品出现在同一组中的概率。
我有以下数据,
data <- data.frame(group = c(1,1,1,1,2,2,2,2,3,3,3,3),
commodity = c("Wheat", "Coal", "Steel", "Iron", "Wheat", "Coal", "Steel", "Iron", "Wheat", "Coal", "Steel", "Iron"),
quantity = c(5,10,0,5,20,5,10,0,0,10,15,15),
proportion = c(0.25,0.5,0,0.25,0.57,0.14,0.29,0,0,0.25,0.375,0.375))
我想计算每个唯一可能的商品对的比例(产品总和除以 2)。
结果应该是这样的,
result <- data.frame(commodity1 = c("Wheat", "Wheat", "Wheat", "Coal", "Coal", "Steel"),
commodity2 = c("Coal", "Steel", "Iron", "Steel", "Iron", "Iron"),
result = c(0.103,0.082,0.031,0.067,0.109,0.070))
例如,小麦 - 煤的结果将计算为 (0.25 * 0.5/2)+(0.57 * 0.14/2)+(0 * 0.25/2)=0.103
我已将商品对隔离到单独的 data.frame 中以将结果变异为并尝试进行 rowwise() 操作。
任何建议将不胜感激。
【问题讨论】:
-
组对中的每个比例乘积除以 2
-
没有一个答案达到预期目的吗?您还没有接受任何答案!
标签: r dplyr statistics rowwise