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 / bibtex_2academic.R
Last active July 6, 2019 11:51 — forked from lbusett/bibtex_2academic.R
script for importing publications from a "bibtex" file to a hugo-academic website
#' @title bibtex_2academic
#' @description import publications from a bibtex file to a hugo-academic website
#' @author Lorenzo Busetto, phD (2017) <lbusett@gmail.com>
#' @modified Peter Paul Pichler (2019) <pichler@pik-potsdam.de>
#' @modified R.Andres Castaneda (2019) <acastanedaa@worldbank.org>
bibtex_2academic <- function(bibfile,
outfold,
abstract = FALSE,
overwrite = FALSE) {
@randrescastaneda
randrescastaneda / dir_size.R
Created September 12, 2019 14:41
Calculate size of directories. Display directory tree and returns complete data frame
# requires data.table and data.tree
dir_size <- function(path = ".",
limit = 25,
sharet = .1) {
# get file from directory path
a <- list.files(path,
all.files = TRUE,
recursive = TRUE,
full.names = TRUE)
@randrescastaneda
randrescastaneda / maxN.R
Last active October 3, 2019 15:50
N highest of vector. This function was created by [Zach](https://stackoverflow.com/users/345660/zach) in this [post](https://stackoverflow.com/questions/2453326/fastest-way-to-find-second-third-highest-lowest-value-in-vector-or-column). I just keep it here for storage purposes.
maxN <- function(x, N=2, decreasing = FALSE){
len <- length(x)
if(N>len){
warning('N greater than length(x). Setting N=length(x)')
N <- length(x)
}
if (decreasing == FALSE) {
sort(x,partial=len-N+1)[len-N+1]
} else {
-sort(-x,partial=len-N+1)[len-N+1]
@randrescastaneda
randrescastaneda / examples_handbook_GMD.do
Created February 20, 2020 14:16
Replicate basic poverty and inequality measures using GMD data from the world Bank
/*==================================================
project: Examples of the handbook using GMD
Author: Andres Castaneda
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 6 Jul 2018 - 12:03
Modification Date:
Do-file version: 01
References:
Output: http://eca/povdata/examples_handbook_GMD.DO
@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
@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 / 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 / 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)
}
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 / 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