如何在 R 中多次根据另一列的类别对 1 列的值求和?

How can i sum values of 1 column based on the categories of another column, multiple times, in R?

我想我的问题有点奇怪,让我试着解释一下。我需要为一个关于粮食供应​​和国际贸易的纵向数据库(连续 29 年)求解一个简单的方程式:(importations-exportations)/(production+importations-exportations)*100[粮农组织的粮食依赖系数方程式]。最大的问题是我的数据库有食品及其利益价值(生产、进口和出口)分解,所以我需要找到一种方法将该等式应用于每年利益价值的总和,所以我每年都能得到我需要的系数

我的数据框如下所示:

element      product   year   value (metric tons)
Production   Wheat     1990   16
Importation  Wheat     1990   2
Exportation  Wheat     1990   1
Production   Apples    1990   80
Importation  Apples    1990   0
Exportation  Apples    1990   72
Production   Wheat     1991   12
Importation  Wheat     1991   20
Exportation  Wheat     1991   0

我想这个解决方案很简单,但我在 R 中不够好,无法自己解决这个问题。非常欢迎您的帮助。

谢谢!

This is a picture of my R session

require(data.table)

# dummy table. Use setDT(df) if yours isn't a data table already
df <- data.table(element = (rep(c('p', 'i', 'e'), 3))
                 , product = (rep(c('w', 'a', 'w'), each=3))
                 , year = rep(c(1990, 1991), c(6,3))
                 , value = c(16,2,1,80,0,72,12,20,0)
                 ); df

   element product year value
1:       p       w 1990    16
2:       i       w 1990     2
3:       e       w 1990     1
4:       p       a 1990    80
5:       i       a 1990     0
6:       e       a 1990    72
7:       p       w 1991    12
8:       i       w 1991    20
9:       e       w 1991     0



# long to wide
df_1 <- dcast(df
              , product + year ~ element
              , value.var = 'value'
              ); df_1

# apply calculation
df_1[, food_depend_coef := (i-e) / (p+i-e)*100][]

   product year  e  i  p food_depend_coef
1:       a 1990 72  0 80      -900.000000
2:       w 1990  1  2 16         5.882353
3:       w 1991  0 20 12        62.500000