计算一列中的百分比变化,从第一年到第一年

时间:2018-01-24 16:51:59

标签: r dplyr percentage

我想计算第一年2015和去年2017之间的百分比变化,作为每个city的一个值。

这是我的可重复示例,其中最后一列perct_change_2015_2017是所需的输出。我如何在R中为一大堆城市做到这一点?优选在dplyr中。

编辑,具有正确的百分比变化数字

example <- structure(list(city = c("Amsterdam", "Amsterdam", "Amsterdam", 
"Rotterdam", "Rotterdam", "Rotterdam"), year = c(2015L, 2016L, 
2017L, 2015L, 2016L, 2017L), value = c(30L, 35L, 46L, 23L, 19L, 
17L), perct_change_2015_2017 = c(0.5333333333, 0.5333333333, 
0.5333333333, -0.2608695652, -0.2608695652, -0.2608695652)), .Names = c("city", 
"year", "value", "perct_change_2015_2017"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
    cols = structure(list(city = structure(list(), class = c("collector_character", 
    "collector")), year = structure(list(), class = c("collector_integer", 
    "collector")), value = structure(list(), class = c("collector_integer", 
    "collector")), perct_change_2015_2017 = structure(list(), class = c("collector_double", 
    "collector"))), .Names = c("city", "year", "value", "perct_change_2015_2017"
    )), default = structure(list(), class = c("collector_guess", 
    "collector"))), .Names = c("cols", "default"), class = "col_spec"))

example

 A tibble: 6 x 4
  city       year value perct_change_2015_2017
  <chr>     <int> <int>                  <dbl>
1 Amsterdam  2015    30                  0.533
2 Amsterdam  2016    35                  0.533
3 Amsterdam  2017    46                  0.533
4 Rotterdam  2015    23                 -0.260
5 Rotterdam  2016    19                 -0.260
6 Rotterdam  2017    17                 -0.260

2 个答案:

答案 0 :(得分:4)

此方法将始终使用20152017,无论有多少年。我更喜欢使用firstlast的www解决方案,但是如果你有更多的年份并想要这些特定的年份,那么这里是如何做的。

example %>% group_by(city) %>%
  mutate(perct_change_2015_2017 =
    (value[year == 2017] - value[year == 2015]) / value[year == 2015]
  )
# # A tibble: 6 x 4
# # Groups:   city [2]
#        city  year value perct_change_2015_2017
#       <chr> <int> <int>                  <dbl>
# 1 Amsterdam  2015    30              0.5333333
# 2 Amsterdam  2016    35              0.5333333
# 3 Amsterdam  2017    46              0.5333333
# 4 Rotterdam  2015    23             -0.2608696
# 5 Rotterdam  2016    19             -0.2608696
# 6 Rotterdam  2017    17             -0.2608696

答案 1 :(得分:2)

firstlast功能可能很有用。使用arrange函数确保值的顺序正确也很重要。

library(dplyr)

example2 <- example %>%
  arrange(city, year) %>%
  group_by(city) %>%
  mutate(perct_change_2015_2017 = (last(value) - first(value))/first(value)) %>%
  ungroup()
example2
# # A tibble: 6 x 4
#   city       year value perct_change_2015_2017
#   <chr>     <int> <int>                  <dbl>
# 1 Amsterdam  2015    30                  0.533
# 2 Amsterdam  2016    35                  0.533
# 3 Amsterdam  2017    46                  0.533
# 4 Rotterdam  2015    23                 -0.261
# 5 Rotterdam  2016    19                 -0.261
# 6 Rotterdam  2017    17                 -0.261

数据

structure(list(city = c("Amsterdam", "Amsterdam", "Amsterdam", 
"Rotterdam", "Rotterdam", "Rotterdam"), year = c(2015L, 2016L, 
2017L, 2015L, 2016L, 2017L), value = c(30L, 35L, 46L, 23L, 19L, 
17L)), .Names = c("city", "year", "value"), row.names = c(NA, 
-6L), spec = structure(list(cols = structure(list(city = structure(list(), class = c("collector_character", 
"collector")), year = structure(list(), class = c("collector_integer", 
"collector")), value = structure(list(), class = c("collector_integer", 
"collector")), perct_change_2015_2017 = structure(list(), class = c("collector_double", 
"collector"))), .Names = c("city", "year", "value", "perct_change_2015_2017"
)), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df", 
"tbl", "data.frame"))