Miscellaneous R functions Karl wanted to reuse. Inspired by kimisc.
For a similar grab-bag of Python and Stata scripts, see https://github.com/karldw/useful_scripts.
remotes ::install_github("karldw/kdw.junk")
box_home
– Find Boxdropbox_home
– Find Dropboxis_id
– Do variables uniquely ID rows? (Works with dplyr::select
syntax for local and remote tables. For local tables, faster when data.table is installed.).ensure_id_vars
– Ensure variables uniquely ID rows (works with dplyr::select
syntax; useful in pipelines).binscatter
– Provide a command like binscatter in Stata, based on binsreg, but with a formula interface and plotting support for fixed effects.capture_plot_output
– Like utils::capture.output()
, when the expression would open a graphics device.configure_tikzDevice
– Prepare to use tikzDevicesave_plot
– Save ggplot
graphic with aspect ratio more appropriate for slides or letter paper (uses cairo_pdf
for better graphics).reproducible
option to reset file timestamps.auto_render
– Render and automatically re-render rmd
, md
, or tex
files.cite_attached_packages
– Generate biblatex citations for the attached packages.clear_all
– Delete everything in session.get_cores
– Get number of cores available, with a reasonable max.get_os
– Get current OS.install_lazy
– Install packages if necessary.is.connection
– Test if object is a connection.make_better_names
– Improve a character vector of names.memory_limit
– Set a memory limit (Linux and Windows only).month_to_quarter
– Convert months to quarters.make_monthly
– Convert date to monthly (day of month set to 1).rename_cols
– Rename columns with a dict-like approach.st_union_intersection
– Union intersecting sf
geometries.stopif
– Inverse of stopifnot
.truncate_bytes
– Truncate a string to a particular length in bytes (not very efficiently).winsorize
– winsorize in a way that handles point masses.read_dta
– Wrapper around haven::read_dta
to fix hassles with attributes and factors.Does what is says on the box.
hol_christmas_day
hol_columbus_day
hol_daylight_saving
hol_easter
hol_george_washington_birthday
hol_good_friday
hol_inauguration_day
hol_independence_day
hol_labor_day
hol_martin_luther_king_day
hol_memorial_day
hol_new_years_day
hol_presidents_day
hol_thanksgiving_day
hol_us_federal_holidays
hol_veterans_day
explain_analyze
pg_add_foreign_key
pg_add_index
pg_add_primary_key
pg_vacuum
read_data
– use rio::import()
instead.lapply_parallel
– use furrr::future_map()
instead.lapply_bind_rows
– use furrr::future_dfr()
instead.vec2string
– you're better off using glue::glue()
most of the time.make_join_safer
– use safejoin instead.dplyr::*_join
function and return a new function that performs additional checks to avoid many-to-many mergesmerge_stata
– use safejoin instead.merge
narrate
– use rlang::inform()
instead.update_packages
– update packages and reinstall everything that depends on Rcpp when Rcpp is updated. Deprecated because that doesn't seem to be an issue anymore.felm_strict
– Run lfe::felm()
strictly.fixest::feols()
insteadAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.