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 / 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 / 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 / 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 / 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
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 June 24, 2020 19:10
Find which names of variables in one dataframe x are in equal to the names in dataframe y
equ_names <- function(x, y) {
nms <- names(x)[names(x) %in% names(y)]
return(nms)
}
@randrescastaneda
randrescastaneda / stata_log.do
Created June 9, 2020 02:32
Stata code to ask someone else to run in their computers so we can check if a particular program is working properly.
***********
cd ~/desktop
log using tech.txt, text replace
set more off
about
sysdir
adopath
creturn list
query compilenumber
query
@randrescastaneda
randrescastaneda / addUnits.R
Last active August 27, 2020 14:12
label large units in charts, source: https://stackoverflow.com/a/52602625/11472481
# labels for charts
addUnits <- function(n, d = 0) {
labels <- ifelse(n < 1000, n, # less than thousands
ifelse(n < 1e6, paste0(round(n/1e3, d), 'k'), # in thousands
ifelse(n < 1e9, paste0(round(n/1e6, d), 'M'), # in millions
ifelse(n < 1e12, paste0(round(n/1e9, d), 'B'), # in billions
'too big!')
)
)
)
@randrescastaneda
randrescastaneda / add_and.R
Last active April 1, 2020 12:14
Add "and" to character vector to be used in string contexts such as Rmarkdown documents
add_and <- function(x) {
if (!(is.character(x))) {
warning("`x` must be character. coercing to character")
x <- as.character(x)
}
lx <- length(x)
if (lx == 1) {
y <- x
}
@randrescastaneda
randrescastaneda / merge_cpi_povcalnet.do
Last active March 27, 2020 18:29
Merge CPI data in datalibweb to povclanet data... work in progress
*##s
pcn load price, clear
keep countrycode year ref_year survname coverage datatype
tempfile pf
save `pf'
pcn load cpi, clear