Я пытался рассчитать годовой темп роста для сгруппированных данных;сгруппированы по 1) группе, 2) категории продукта и 3) году.
Я попытался сгруппировать по трем параметрам, а затем вычислить скорость роста как: ((x / dplyr :: lag (x, 1))-1) * 100.Тем не менее, эта операция приводит к появлению NA только для нового столбца.
group_exports_g.rate <- baci_exports %>% ungroup() %>%
group_by(group,sna,t) %>% summarise(exports = sum(exports),
n= sum(n)) %>% ungroup() %>%
group_by(group,sna,t) %>% arrange(group,sna,t) %>%
mutate(gr.exports = 100*((exports/lag(exports,1))-1))
dput(baci_exports)
structure(list(t = c(1995, 1995, 1995, 1995, 1995, 1995, 1995,
1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995,
1995, 1995), i = c(4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
8, 12, 12, 12, 12, 12), sna = c("Capital goods", "Consumer goods",
"Fuels and lubricants", "Intermediate goods", "Parts and accessories of capital goods",
"Passenger motor cars", "Primary goods", "Capital goods", "Consumer goods",
"Fuels and lubricants", "Goods not elsewhere specified", "Intermediate goods",
"Parts and accessories of capital goods", "Passenger motor cars",
"Primary goods", "Capital goods", "Consumer goods", "Fuels and lubricants",
"Intermediate goods", "Parts and accessories of capital goods"
), exports = c(1195.2624224154, 22997.0533036558, 5.3693833059,
9720.564817782, 183.9809856813, 111.2556313572, 47976.2051093033,
5711.1685900189, 120659.569187797, 2264.5410811663, 75.806, 60184.2530566294,
2249.821, 280.439, 36458.4499193458, 30316.017597217, 150361.471507771,
8686043.01945958, 448718.59940394, 6633.9115459232), n = c(36L,
207L, 1L, 137L, 30L, 5L, 58L, 143L, 654L, 5L, 3L, 322L, 57L,
12L, 143L, 270L, 364L, 106L, 563L, 150L), name_baci = c("Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Albania", "Albania", "Albania", "Albania", "Albania",
"Albania", "Albania", "Albania", "Algeria", "Algeria", "Algeria",
"Algeria", "Algeria"), cntry = c("Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Albania", "Albania", "Albania", "Albania", "Albania", "Albania",
"Albania", "Albania", "Algeria", "Algeria", "Algeria", "Algeria",
"Algeria"), group = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "SE",
"SE", "SE", "SE", "SE", "SE", "SE", "SE", "Algeria", "Algeria",
"Algeria", "Algeria", "Algeria")), row.names = c(NA, -20L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), groups = structure(list(t = c(1995,
1995, 1995), i = c(4, 8, 12), .rows = list(1:7, 8:15, 16:20)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
>
structure(list(t = c(1995, 1996, 1997, 1998, 1999, 2000, 2001,
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
2013, 2014, 2015, 2016, 2017), i = c(4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), sna = c("Capital goods",
"Capital goods", "Capital goods", "Capital goods", "Capital goods",
"Capital goods", "Capital goods", "Capital goods", "Capital goods",
"Capital goods", "Capital goods", "Capital goods", "Capital goods",
"Capital goods", "Capital goods", "Capital goods", "Capital goods",
"Capital goods", "Capital goods", "Capital goods", "Capital goods",
"Capital goods", "Capital goods"), exports = c(1195.2624224154,
1487.4614064276, 1525.1489543903, 1598.3509917338, 2687.9284436967,
1754.2923408387, 5913.8963941332, 2619.5146133123, 2915.904116471,
6412.63136988, 8158.0444400432, 4312.0519543819, 31170.4665315818,
39146.7973036179, 184970.163402516, 20611.8847778549, 28046.1267778067,
12125.1990587805, 15435.0095479273, 15942.6566817083, 10549.8178035657,
9092.5422325593, 7967.3875079918), n = c(36L, 49L, 71L, 50L,
64L, 88L, 107L, 76L, 105L, 138L, 191L, 178L, 175L, 264L, 448L,
306L, 210L, 232L, 306L, 280L, 207L, 180L, 198L), name_baci = c("Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan"), cntry = c("Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan"), group = c("Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",
"Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan"
)), row.names = c(NA, -23L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), groups = structure(list(t = c(1995, 1996,
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017),
i = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4), .rows = list(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L)), row.names = c(NA, -23L), class = c("tbl_df",
"tbl", "data.frame")))
Я хотел бы иметь ежегодные темпы роста (процентное изменение) для группы, категории продукта и года.