Skip to content

Instantly share code, notes, and snippets.

View randrescastaneda's full-sized avatar
😊
I may be slow to respond.

R.Andrés Castañeda randrescastaneda

😊
I may be slow to respond.
View GitHub Profile
@randrescastaneda
randrescastaneda / using_deparse.R
Last active July 21, 2020 21:12
How to use deparse in preparing conditions to by used in tidyverse of dplyr functions
library(tidyverse)
# Original data
tb <- tibble(
x = c("a", "b", "c", "d"),
y = c(1, 2, 3, 4)
)
# Regular condition
@randrescastaneda
randrescastaneda / qtile.R
Created July 21, 2020 21:34
Create quantiles based on size of sample (This is not the same as percentiles)
qtile <- function(x, nq = 10) {
N <- length(x)
csw <- 1:N
qp <- floor(csw/((N+1)/nq)) + 1
return(qp)
}
@randrescastaneda
randrescastaneda / NP_xml_files.do
Last active July 30, 2020 12:40
Download XML files from repo
local dir "c:\Users\wbxxxxx\AppData\Roaming\Notepad++"
local source "https://raw.githubusercontent.com/randrescastaneda/NPplusplus_to_Stata/master"
local files "shortcuts.xml stylers.xml userDefineLang.xml"
foreach file of local files {
copy "`source'/`file'" "`dir'/`file'", replace
}
@randrescastaneda
randrescastaneda / microbenchmark_template.R
Created March 10, 2021 17:41
microbenchmark template in R with nice viz using highcharter
bench <- microbenchmark::microbenchmark(
times = 100,
one = ,
two =
)
if (requireNamespace("highcharter")) {
hc_dt <- highcharter::data_to_boxplot(bench,
time,
expr,
add_outliers = FALSE,
@randrescastaneda
randrescastaneda / compare_pip_versions.R
Created May 4, 2022 20:43
Compare versions of PIP
# ____________________________________________________________________________
# country level ####
sr0 <- pipr::get_stats("SYR", fill_gaps = TRUE, server = "dev")
sr1 <-
pipr::get_stats("SYR",
fill_gaps = TRUE,
server = "dev",
version = "20220428_2011_02_02_PROD")
@randrescastaneda
randrescastaneda / extra_vars.R
Last active August 20, 2022 15:02
Find Variables available in one data.frame not available in another.
#' Find variable that are not available in one on another data.frame
#'
#' @param x data.frame 1
#' @param y data.frame 2
#'
#' @return
#' @export
#'
#' @examples
extra_vars <- function(x, y) {
@randrescastaneda
randrescastaneda / replace_text_in_file.R
Created November 22, 2022 22:16
replace the same text pattern in different files
rfile <- "R/pip_indicators.R"
rfiles <- fs::dir_ls("R", type = "file", regexp = "[rR]$")
srch <- 'msrdir <- fs::path\\(maindir, "_aux", branch, measure\\)'
rplc <-
"if (branch == \"main\") {
branch <- \"\"
}
msrdir <- fs::path(maindir, \"_aux\", branch, measure)"
@randrescastaneda
randrescastaneda / parse_endpoints.R
Created June 7, 2023 15:38
Parse PIP API endpoints documentation to text file
library(data.table)
url <- "https://raw.githubusercontent.com/PIP-Technical-Team/pipapi/master/inst/plumber/v1/endpoints.R"
lns <- readLines(con = url) |>
{\(.) .[grep("\\@(get|param)", .)]}()
@randrescastaneda
randrescastaneda / lineup_testing.R
Last active June 28, 2023 15:05
Test lineup dataset for group data and imputed data. Check with current PIP API
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# load packages ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# library(dtplyr) # worth talking about this.
library(data.table)
library(tidyverse)
# If you don't have installed some of the packages that I use below you, it is
# likely that Rstudio is going to let you know that you need to install them.
@randrescastaneda
randrescastaneda / IND_2016_deciles_lineup_replicate.R
Created July 6, 2023 22:52
Replicate PIP deciles using imputes data with weights adjusted
library(data.table)
library(tidyverse)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# load data ---------
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
path <- fs::path("p:/03.pip/estimates/lineup/IND_2016_survey_year.fst")
sdt <- fst::read_fst(path = path)