Is there any library in R where you can indicate the number of rows to add (2,3,4,600 etc) and that adds intermediate data between rows?
For example, I have this data:
df <- read.table(text = '
municipio año poblacion
mun1 2000 100
mun1 2001 200
mun1 2002 300
mun2 2000 1550
mun2 2001 500
mun2 2002 3000
', header = TRUE, stringsAsFactors = FALSE)
I want to add for example 2 extra rows between the data proportionally.
That is, enter 2 data between 100 and 200.
It would be approximately 100-133-166-200. You have entered 133 and 166 as new data.
Between the data 1500 and 500 it would be (1500-500)/3 = 350. Well, the new data would go from 350 to 350:
1500-1200-850-500
In the end it would be something like this:
It occurs to me that you could solve it like this:
Comments:
arrange()
municipio
rows_by_group
definedleft_join
with itdata.frame
to obtain the population of the following yearAn alternative using
seq
andunnest
.The idea is simple, separate by groups, generate a list of sequences of length 4 between each population value and the next, and unnest the sequence.
It has a complication:
seq()
it doesn't work with NA as other functions usually doR
, it gives an error instead of returning NA. So inlead
it I have to indicate that the default is 0 and generate unnecessary sequences that I have to filter later. It is solved with a filter and should not be problematic as long as there are no municipality/year combinations with the observed (real) value 0.The other complication is that values are repeated because they are at the end of one of the sequences and at the beginning of the following ones. Again, very ad hoc filter: remove a row if it is identical to the next. Obvious bug: the next row is REALLY the same because two combinations of municipality/year are the same.