R:在计算一列相对于另一列的值时,如何使用聚合?

时间:2018-12-04 18:31:51

标签: r aggregate data-science

那是我们的练习:

“对于此任务,您将使用R附带的LifeCycleSavings数据集。该数据集显示了1960年至1970年之间不同国家的储蓄率,并提供了有关15岁以下国家人口百分比的信息并超过75岁。 a)(3个百分点)计算每个国家/地区15岁以下人口每百分之一的可支配收入的平均增长率。 b)(5 Pkt。)根据增长率对刚计算出的信息进行排序,并使用条形图将其可视化。一定要标记剧情 正确。”

我不知道这是

LifeCycleSavings = LifeCycleSavings
LifeCycleSavings$ddpi_per_under15 <-(LifeCycleSavings$ddpi/LifeCycleSavings$pop15) * 100

#b
LifeCycleSavings <- LifeCycleSavings[order(LifeCycleSavings[, 6]), ] 

par(mar=c(13, 6, 6, 1));
barplot(LifeCycleSavings$ddpi_per_under15, names=rownames(LifeCycleSavings), las =2,
    xlim=c(0,60), ylim = c(0,100), col = terrain.colors(15),
    main = "average growth rate of disposable income
    per percentage of the population under 15 of each country (1960-1970)",
    xlab = "", ylab = "average growth rate of disposable income 
    per percentage of pop. under 15 (ratio)", font.lab = 2)
    mtext("countries", side=1, line=8, font = 2)

或此版本是解决此问题的正确方法:

ddpi_per_under15 = aggregate(ddpi~pop15,LifeCycleSavings, mean)
row.names(ddpi_per_under15) = row.names(LifeCycleSavings)

#b

average_growth15_sorted <- ddpi_per_under15[order(ddpi_per_under15$ddpi), ]

barplot(average_growth15_sorted$ddpi, 
    names.arg = rownames(average_growth15_sorted),
    main = "Average growth of the disposable income
    per percentage of the population under 15 of each country", 
    ylab = "Average growth rate", xlab = "", las = 2,
    ylim = c(0,20), col=rainbow(50))

那么,这个问题要求比率吗?或“每”在这种情况下是什么意思? 谢谢!

0 个答案:

没有答案