我想试着计算一个分数,或者创建一个指数,给出世界经济中国家“边缘化”的程度。换句话说,这一指数将表明国家在世界经济中的地位。
基本上,我试着复制别人的指示器。他将世界经济中的“边缘化”定义为该国在世界贸易中所占的百分比。
计算方法是:进口总额+某一国家出口总额除以进口总额+世界经济出口总额。除以100的反比。
我试着用我的数据,用R来计算这个。下面我举一个(真实的)例子。
Country Year EXPORT(%GDP) IMPORT(%GDP) GDP(current$)
A 2001 22,14 21,96 3,78(+11)
B 2001 35,43 31,80 5387293(..)
C 2001 27,22 30,84 1,90
WORLD 2001 24,43 24,20 3,30(+13)为了明确起见,我想在我的数据中为每个国家和年份计算一个分数(大约有150个国家,从1990年到2014年)。等式是(再次明确):进口+出口(数据集中某一国家)/进口+出口(世界经济,请参阅示例中的变量" world“)。
编辑:当前$的另一个示例(如果这有任何帮助)
Country Year EXPORT(current$) IMPORT(c$) GDP(c$)
A 2001 8,38177(..) 8,31506 3,78(+11)
B 2001 1,90875(..) 1,71328 5,387293(..)
C 2001 5,1872(..) 5,87710 1,90
WORLD 2001 7,6811(..) 7,7101 3,30(+13)发布于 2015-11-04 17:24:08
使用好的旧data.table:
library( data.table)
# Thanks "TheKevinNeville" for the sample data!
country <- c("A", "B", "C", "World")
year <- c(rep(2001, 4), rep(2002, 4))
export <- abs(rnorm(8) * 5)
import <- abs(rnorm(8) * 5)
dt <- data.table( country,year,export,import)
# Here you calculate the index (rank) per group
dt[,index := (import + export) / .SD[country=="World", import + export],by=.(year)]结果如下:
country year export import index
1: A 2001 4.641794 7.3788739 6.222089
2: B 2001 4.286842 1.3656420 2.925816
3: C 2001 1.919439 1.1210429 1.573802
4: World 2001 1.164199 0.7677355 1.000000
5: A 2002 1.303430 3.5848178 1.478056
6: B 2002 4.231528 2.6427575 2.078573
7: C 2002 8.655763 7.1272979 4.772314
8: World 2002 2.134707 1.1725057 1.000000如果要按年排序结果和索引(降序),可以添加以下代码:
# setorder reorders the rows of a data.table by reference,
# based on the columns provided.
setorder(dt, year, -index)发布于 2015-11-04 06:09:37
创建数据。
country <- c("A", "B", "C", "World")
year <- c(rep(2001, 4), rep(2002, 4))
export <- abs(rnorm(8) * 5)
import <- abs(rnorm(8) * 5)
mydf <- data.frame(country=country,Year=year,EXPORT=export, IMPORT=import)为循环。
mydf$Score <- NA
for(i in 2001:2002){
index <- mydf[,"Year"] == i
current_world <- mydf$country[index] == "World"
mydf$Score[index] <- (mydf$EXPORT[index] + mydf$IMPORT[index]) / (mydf$EXPORT[index][current_world] + mydf$IMPORT[index][current_world])
}https://stackoverflow.com/questions/33514163
复制相似问题