Skip to content

Instantly share code, notes, and snippets.

Avatar
😊
I may be slow to respond.

R.Andrés Castañeda randrescastaneda

😊
I may be slow to respond.
View GitHub Profile
@randrescastaneda
randrescastaneda / replace_text_in_file.R
Created Nov 22, 2022
replace the same text pattern in different files
View replace_text_in_file.R
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 / extra_vars.R
Last active Aug 20, 2022
Find Variables available in one data.frame not available in another.
View extra_vars.R
#' 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) {
View compare_pip_versions.R
# ____________________________________________________________________________
# 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 / microbenchmark_template.R
Created Mar 10, 2021
microbenchmark template in R with nice viz using highcharter
View microbenchmark_template.R
bench <- microbenchmark::microbenchmark(
times = 100,
one = ,
two =
)
if (requireNamespace("highcharter")) {
hc_dt <- highcharter::data_to_boxplot(bench,
time,
expr,
add_outliers = FALSE,
@randrescastaneda
randrescastaneda / NP_xml_files.do
Last active Jul 30, 2020
Download XML files from repo
View NP_xml_files.do
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 / qtile.R
Created Jul 21, 2020
Create quantiles based on size of sample (This is not the same as percentiles)
View qtile.R
qtile <- function(x, nq = 10) {
N <- length(x)
csw <- 1:N
qp <- floor(csw/((N+1)/nq)) + 1
return(qp)
}
@randrescastaneda
randrescastaneda / using_deparse.R
Last active Jul 21, 2020
How to use deparse in preparing conditions to by used in tidyverse of dplyr functions
View using_deparse.R
library(tidyverse)
# Original data
tb <- tibble(
x = c("a", "b", "c", "d"),
y = c(1, 2, 3, 4)
)
# Regular condition
View ord_nums.R
ord_nums <- function(n){
ord <- ifelse(n %in% c(11,12,13), "th",
ifelse(
n %% 10 == 1, 'st',
ifelse(
n %% 10 == 2, 'nd',
ifelse(
n %% 10 == 3 , 'rd', "th"
@randrescastaneda
randrescastaneda / equ_names.R
Created Jun 24, 2020
Find which names of variables in one dataframe x are in equal to the names in dataframe y
View equ_names.R
equ_names <- function(x, y) {
nms <- names(x)[names(x) %in% names(y)]
return(nms)
}
@randrescastaneda
randrescastaneda / stata_log.do
Created Jun 9, 2020
Stata code to ask someone else to run in their computers so we can check if a particular program is working properly.
View stata_log.do
***********
cd ~/desktop
log using tech.txt, text replace
set more off
about
sysdir
adopath
creturn list
query compilenumber
query