Created
March 16, 2023 12:57
-
-
Save JosepER/781a451a8dcc3a10f5c847f574a232bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
national_accounts_df <- tibble::tibble(country = c("ITA", "ITA", "ITA", "ITA", "ITA"), | |
variable = c("NFD41R", "NFD11P", "NFB3GR", "NFD42R", "NFD45R"), | |
sector = c("S14", "S14", "S14", "S14", "S14"), | |
year = c("2016", "2016", "2016", "2016", "2016"), | |
value = c(27593.2, 84325810, 227852.9, 119509.5, 1034.7)) | |
estimates_from_microdata_df <- tibble::tibble(indicator = c("D11P", "B3GR+D41R+D42R+D45R"), | |
value = c(84328052, 894699.3)) | |
compute_national_account_ratios <- function(estimates_from_microdata_df, national_accounts_table){ | |
national_accounts_table <- national_accounts_df | |
national_accounts_table$variable <- stringr::str_remove(national_accounts_df$variable, "^NF" ) | |
national_accounts_table_wide <- national_accounts_table %>% | |
pivot_wider(id_cols = "country", names_from = "variable", values_from = "value") | |
national_accounts_estimates <- purrr::map_dbl(estimates_from_microdata_df[["indicator"]], | |
.f = function(formula_str, df){ | |
formula_expr <- parse(text = formula_str) | |
# Evaluate the formula expression within the context of the data.frame | |
new_column <- eval(formula_expr, envir = df) | |
return(new_column) | |
}, | |
national_accounts_table_wide) | |
return(estimates_from_microdata_df[["value"]]/national_accounts_estimates) | |
} | |
compute_national_account_ratios(estimates_from_microdata_df, national_accounts_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment