计算R中的百分比变化
我需要计算一个国家的人口变化百分比。此外,我需要将不同年份的行相加为每年一行,而不是12行。我尝试了以下代码,但不知道如何处理年份部分计算R中的百分比变化,r,dplyr,R,Dplyr,我需要计算一个国家的人口变化百分比。此外,我需要将不同年份的行相加为每年一行,而不是12行。我尝试了以下代码,但不知道如何处理年份部分 structure(list(country = c("Albania", "Albania", "Albania", "Albania", "Albania", "Albania", "Albania"
structure(list(country = c("Albania", "Albania", "Albania", "Albania",
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania",
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania",
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania",
"Albania", "Albania"), year = c(1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L,
1986L), population = c(277900, 246800, 267500, 298300, 138700,
34200, 301400, 264200, 296700, 325800, 132500, 21100, 283900,
252100, 273200, 304700, 141700, 34900, 306700, 269000, 302000,
331600, 134800, 21400), pct.chg = c(NA, -11.1910759265923, 8.38735818476499,
11.5140186915888, -53.5031847133758, -75.3424657534247, 781.286549707602,
-12.342402123424, 12.3012869038607, 9.80788675429727, -59.3308778391651,
-84.0754716981132, NA, -11.2011271574498, 8.36969456564855, 11.5300146412884,
-53.495241220873, -75.3705010585744, 778.796561604585, -12.292142158461,
12.2676579925651, 9.80132450331126, -59.3486127864897, -84.1246290801187
)), row.names = c(NA, -24L), groups = structure(list(year = 1985:1986,
.rows = structure(list(1:12, 13:24), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
df <- comp %>%
filter(country == 'Albania') %>%
select(country, year, population) %>%
group_by(year) %>%
mutate(pct.chg = 100 * (population - lag(population))/lag(population))
[1]: https://i.stack.imgur.com/U3d2E.jpg
结构(清单)(国家=c(“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”,
“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”,
“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”,
“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”、“阿尔巴尼亚”,
“阿尔巴尼亚”,“阿尔巴尼亚”,年份=c(1985L,1985L,1985L,1985L,1985L,
1985L,1985L,1985L,1985L,1985L,1985L,1985L,1986L,1986L,
1986L,1986L,1986L,1986L,1986L,1986L,1986L,1986L,1986L,1986L,
人口=c(277900246800267500298300138700,
34200, 301400, 264200, 296700, 325800, 132500, 21100, 283900,
252100, 273200, 304700, 141700, 34900, 306700, 269000, 302000,
3316013480021400),pct.chg=c(NA,-11.1910759265923,8.38735818476499,
11.5140186915888, -53.5031847133758, -75.3424657534247, 781.286549707602,
-12.342402123424, 12.3012869038607, 9.80788675429727, -59.3308778391651,
-84.0754716981132,NA,-11.2011271574498,8.369694564855,11.5300146412884,
-53.495241220873, -75.3705010585744, 778.796561604585, -12.292142158461,
12.2676579925651, 9.80132450331126, -59.3486127864897, -84.1246290801187
)),row.names=c(NA,-24L),groups=structure(list)(年份=1985:1986,
.rows=structure(list(1:12,13:24),ptype=integer(0),class=c(“vctrs\u list\u of”,
“vctrs_vctr”,“list”)),row.names=1:2,class=c(“tbl_df”,
“tbl”,“data.frame”),.drop=TRUE),class=c(“分组的”,
“tbl_df”、“tbl”、“data.frame”))
df%
筛选器(国家=='阿尔巴尼亚')%>%
选择(国家、年份、人口)%>%
组别(年份)%>%
突变(pct.chg=100*(总体-滞后(总体))/滞后(总体))
[1]: https://i.stack.imgur.com/U3d2E.jpg
也许,我们需要总结
library(dplyr)
comp %>%
filter(country == 'Albania') %>%
select(country, year, population) %>%
group_by(year) %>%
summarise(pct.chg = sum(100 * (population - lag(population))/lag(population)))
也许,我们需要总结
library(dplyr)
comp %>%
filter(country == 'Albania') %>%
select(country, year, population) %>%
group_by(year) %>%
summarise(pct.chg = sum(100 * (population - lag(population))/lag(population)))
欢迎来到堆栈溢出。请以纯文本格式包含示例数据-例如,dput(yourdata)
的输出。我们无法从图像复制/粘贴数据。您每年有多行。在最终输出中,您希望每年只有一行吗?您希望对所有总体值执行什么操作?拿他们的总数来说,你是说?您是否需要comp%>%filter(country=='albana')%%>%groupby(year)%%>%summarsePop=sum(population))
?欢迎使用堆栈溢出。请以纯文本格式包含示例数据-例如,dput(yourdata)
的输出。我们无法从图像复制/粘贴数据。您每年有多行。在最终输出中,您希望每年只有一行吗?您希望对所有总体值执行什么操作?拿他们的总数来说,你是说?您是否需要comp%>%filter(country=='albana')%%>%groupby(year)%%>%summarsepop=sum(population))
?它显示错误summary()
解组输出(用.groups
参数覆盖)百分比变化的结果在所有行中都是NA。@Ama你能用该示例数据的dput
更新你的帖子吗?这样我就可以验证我不认为在百分比计算之后应该进行加法。这一指标毫无意义。也许,summary(population=sum(population))%%>%mutate(pct=100*(population lag(population))/lag(population))
就是@Ama:要找的东西?它显示了错误summary()
解组输出(用.groups
参数覆盖)百分比变化的结果在所有行中都是NA。@Ama你能用该示例数据的dput
更新你的帖子吗?这样我就可以验证我不认为在百分比计算之后应该进行加法。这一指标毫无意义。也许,summary(population=sum(population))%%>%mutate(pct=100*(population lag(population))/lag(population))
就是@Ama:想要的?