df1 %>%
group_by(reporter, year) %>%
summarize(TI = sum(trade_value_usd[df1 != "World"], na.rm = T) / sum(trade_value_usd[df1$partner == "World"], na.rm = T) /
(sum(df2$trade_value_usd) / sum(df3$trade_value_usd)))
我需要df2在这样的操作中遵循df1的标准,我该怎么做?
根据你的具体任务,你可以定义一个函数,然后使用 df1
和 df2
作为论据。
manipulator <- function(data) {
output <- data %>%
group_by(reporter, year) %>%
summarize(TI = sum(trade_value_usd[data != "World"], na.rm = T) / sum(trade_value_usd[data$partner == "World"], na.rm = T) /
(sum(df2$trade_value_usd) / sum(df3$trade_value_usd)))
return(output) # or print or something else
}
manipulator(df1)
manipulator(df2)