【问题标题】:Calculate the percentage change in R计算 R 的百分比变化
【发布时间】:2020-11-24 08:37:47
【问题描述】:

我需要计算仅阿尔巴尼亚国家的人口变化百分比。另外,我需要将不同年份的行相加,而不是 12 年。我尝试了以下代码,但我不知道如何处理年份部分。


    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

【问题讨论】:

  • 欢迎来到 Stack Overflow。请make this question reproducible 以纯文本格式包含示例数据 - 例如来自dput(yourdata) 的输出。我们无法从图像中复制/粘贴数据。
  • 您每年都有多行。在最终输出中,您每年只需要一行?您想对所有 population 值做什么?拿他们的总和,是什么意思?你需要comp %&gt;% filter(country == 'Albania') %&gt;% group_by(year) %&gt;% summarisepop = sum(population)) 吗?

标签: r dplyr


【解决方案1】:

也许,我们需要summarise

library(dplyr)
comp %>% 
    filter(country == 'Albania') %>%
    select(country, year, population) %>% 
    group_by(year) %>% 
    summarise(pct.chg = sum(100 * (population - lag(population))/lag(population)))

【讨论】:

  • 它显示错误summarise()取消分组输出(用.groups参数覆盖)并且百分比变化的结果在所有行中都是NA。
  • @Ama 你能用 dput 的示例数据更新你的帖子,以便我可以测试
  • 我不认为应该在百分比计算之后进行加法。该指标毫无意义。也许,summarise(population = sum(population)) %&gt;% mutate(pct=100*(population-lag(population))/lag(population)) 是 @Ama: 正在寻找的东西?
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 2018-06-20
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2021-11-30
相关资源
最近更新 更多