在其他数据框中检索值以进行计算

时间:2019-02-14 16:32:01

标签: r

我有一个类似的df:

df1 <- data.frame(country = c("C1","C1","C2","C2"),year = c(1998,2001,1998,2001), amount = c(11000,11500,5000,4100))

我根据第一个创建了另一个df,如下所示:

df2 <- aggregate(amount ~ year, df1, sum)

我将创建一个新列df1$ratio,该列与每年每个ID的数量比率相对应。它应该看起来像:

df3 <- data.frame(country = c("C1","C1","C2","C2"),year = c(1998,2001,1998,2001), amount = c(11000,11500,5000,4100), ratio = c(.6875, .7372,.3125,.2628))

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

可以使用ave中的base R来完成两步操作,而不用执行两步操作

df1$ratio <- with(df1, amount/ave(amount, year, FUN = sum))

或者使用mutate中的dplyr

library(dplyr)
df1 %>%
    group_by(year) %>%
    mutate(ratio = amount/sum(amount))