Complete values of a column with values of the column itself in date.table

Asked

Viewed 23 times

1

I have a data.table() with the following columns: municipality, year and pib_per_capta. (I actually have 30 more columns, but for example I think this is enough)

The base is fully filled from 2005 to 2018. However, for the year 2019 pib_per_capta is as IN.

I would like to assign value to it in the following way: get the variation rate of 2017 and 2018, and multiply by the value of 2018.

How would that look?

Note: 2019 = (2018/2017) * 2018 Note: If you know how to improve the title of the question, just talk

Data example:

structure(list(municipio = c(110001L, 110001L, 110001L, 110037L, 
110037L, 110037L, 110040L, 110040L, 110040L, 110034L, 110034L, 
110034L), ano = c(2017L, 2018L, 2019L, 2017L, 2018L, 2019L, 2017L, 
2018L, 2019L, 2017L, 2018L, 2019L), pib_per_capta = c(19081.43, 
21552.47, NA, 23353.6, 21053.93, NA, 14699.7, 15655.57, NA, 15062.51, 
17423.49, NA)), row.names = c(NA, -12L), class = c("data.table", 
"data.frame"))

1 answer

1


Here’s a solution. Use a function f to calculate the new value of pib_per_capta (missing a i in capita).

f <- function(x){
  y <- x[["pib_per_capta"]]
  pib <- tail(y, n = 3)[-3]
  new_pib <- pib[2]^2/pib[1]
  pib <- c(y[-length(y)], new_pib)
  pib
}

dados[, pib_per_capta := f(.SD), by = municipio]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.