Skip to content

Instantly share code, notes, and snippets.

@JosepER
JosepER / lissyrtools_example.R
Last active September 15, 2023 08:55
lissyrtools example
library(lissyrtools)
library(magrittr)
files_h <- read_lissy_files(c("ca14h", "ca15h", "ca16h", "ca17h", "ca18h", "ca19h"),
col_select = "dhi")
files_p <- read_lissy_files(c("ca14p", "ca15p", "ca16p", "ca17p", "ca18p", "ca19p"),
col_select = c("pi11", "age"))
lissy_datasets <- merge_dataset_levels(files_h, files_p)
@JosepER
JosepER / reading_multiple_datasets_lissy.R
Created May 24, 2023 07:50
Examples of how to read multiple files in LISSY
# This is an example of how to read multiple files at once in LISSY
# To read multiple years of one country:
library(tidyverse)
years <- c("08", "10", "14", "16")
library(tidyverse)
# Exercise 1
years <- c("08", "10", "14", "16")
# append all IT datasets from 2008 to 2016
it_df <- purrr::map_dfr(.x = years,
.f = ~read.LIS(glue::glue("it{.x}p"), vars = c("year", "educlev", "educ_c", "enroll", "sex", "age" )))
table(it_df$educlev, it_df$educ_c)
# Summer workshop - Data preparation in R --------
# COPY TO LISSY: ----------------------------------------------------------
library(tidyverse)
library(magrittr)
# install.packages("Hmisc")
library(tidyverse)
library(haven)
#' Download the OECD detailed National Accounts
#'
#' @description
#' Uses the API to the OECD tables to download the National Accounts.
#'
#' Downloads all variables for all years for the selected countries.
#'
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))
library(rlang)
library(dplyr)
it14ih <- data.frame(hid = 1:6,
hi11 = c(3601, 13190, 0, 11479, 0, 4713),
hi13 = c(973, 0, 0, 478, 815, 399),
hi12 = c(0, 0, 0, 0, 0, 842),
hicapital = c(40, 41, 0, 100, 0, 83),
hpopwgt = c(567.1, 5308.3, 666.7,
692.2, 254.2, 632.6)
@JosepER
JosepER / example_api_to_oecd_nas.R
Created March 8, 2023 11:32
Example of functions to get OECD National Account data
#' Download the OECD detailed National Accounts
#'
#' @description
#' Uses the API to the OECD tables to download the National Accounts.
#'
#' Downloads all variables for all years for the selected countries.
#'
#' @details
#' The OECD API takes countries with ISO3. The function uses the METIS 'country'
#' table to convert ISO2 to ISO3.
import cppyy
import numpy as np
# 'translation' of the Python function below done by ChatGPT
cppyy.cppdef("""
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
v <- c(8,5,1,3,5,6,7,6,3) # income
w <- seq(0.1, 0.9, 0.1) # weights
w <- w[order(v)]
v <- sort(v)
# compute πₖ