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 Dropbox
is_id
– Do variables uniquely ID rows? (Works withdplyr::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 withdplyr::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
– Likeutils::capture.output()
, when the expression would open a graphics device.configure_tikzDevice
– Prepare to use tikzDevicesave_plot
– Saveggplot
graphic with aspect ratio more appropriate for slides or letter paper (usescairo_pdf
for better graphics).- Offers a
reproducible
option to reset file timestamps.
- Offers a
auto_render
– Render and automatically re-renderrmd
,md
, ortex
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 intersectingsf
geometries.stopif
– Inverse ofstopifnot
.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 aroundhaven::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
– userio::import()
instead.lapply_parallel
– usefurrr::future_map()
instead.lapply_bind_rows
– usefurrr::future_dfr()
instead.
vec2string
– you're better off usingglue::glue()
most of the time.make_join_safer
– use safejoin instead.- Takes a
dplyr::*_join
function and return a new function that performs additional checks to avoid many-to-many merges
- Takes a
merge_stata
– use safejoin instead.- Defined a merge function that mirrored Stata's
merge
- Defined a merge function that mirrored Stata's
narrate
– userlang::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
– Runlfe::felm()
strictly.- Use
fixest::feols()
instead
- Use