2
Dear and esteemed,
I need to calculate the relative population growth of Brazilian municipalities, from one year to the next (final population minus the initial population, divided by the initial population). For that, I did a simple function:
var_rel <- function(x,y){
((x-y)/y)*100
}
In which x
is the population of 2019 and y
the population of 2014.
However, I am not managing to apply a command that runs the function by county. This is because I need to ensure that the x
(population 2019) and y
(2014 population) are from the same municipality.
Today, my function is applied as follows:
var_rel(pop[pop$ano=="2019","qtd_pop_mun"], pop[pop$ano=="2014","qtd_pop_mun"])
However, it may occur to be used as x
the 2019 population of a municipality and how y
the 2014 population of another municipality. Thus, I need to ensure that this does not occur.
I tried to use the function tapply
, but as the function I created var_rel
has arguments x
and y
, I don’t know how to include both in tapply
.
My object has a column for year (from 2014 to 2019), another pro population size and another for the IBGE code of municipality (5570 municipalities of Brazil).
I need to solve this using the function I created for the calculation of the relative variation, because at other times I will have more customized calculations, in which there will be need to generate a function of its own, as I did at that time.
Thank you!
ano key_cd7_ibge_mun qtd_pop_mun qtd_pop_est qtd_pop_pais
1 2014 1100015 25652 1748531 202768562
2 2015 1100015 25578 1768204 204450049
3 2016 1100015 25506 1787279 206081432
4 2017 1100015 25437 1805788 207660929
5 2018 1100015 23167 1757589 208494900
6 2019 1100015 22945 1777225 210147125
7 2014 1100023 102860 1748531 202768562
8 2015 1100023 104401 1768204 204450049
9 2016 1100023 105896 1787279 206081432
10 2017 1100023 107345 1805788 207660929
11 2018 1100023 106168 1757589 208494900
12 2019 1100023 107863 1777225 210147125
13 2014 1100031 6424 1748531 202768562
14 2015 1100031 6355 1768204 204450049
15 2016 1100031 6289 1787279 206081432
16 2017 1100031 6224 1805788 207660929
17 2018 1100031 5438 1757589 208494900
18 2019 1100031 5312 1777225 210147125
Can you please, edit the question with the departure of
dput(pop[c("pop", "qtd_pop_mun")])
or, if the base is too large,dput(head(pop[c("pop", "qtd_pop_mun")], 20))
?– Rui Barradas
"pop" is just the object name, there is no column with that name. I edited with the first 20 rows of the object and all columns.
– Gabriel Barni
You’re absolutely right, I meant
pop[c("ano", etc)]
.– Rui Barradas
key_cd7_ibge_mun
is the county code? 2) There are only lines withano == 2014
, can remake the data example in such a way that there are data of both years for the same municipalities?– Rui Barradas
Yes, this column is the code of the municipality. I have data from 2014 to 2019. The same municipality repeats 6 times, one for each year.
– Gabriel Barni