library(tinytools)
This package contains a set of various small general purpose functions that may be helpful for your daily R coding problems.
# Install development version from GitHub
devtools::install_github('a-maldet/tinytools', build_opts = NULL)
stringify()
: Turn a vector into a single string. Useful for printing vector data in error or warning messages messages.shift_vec()
: Shift the entries of a vector to the left or to the right.format_abs()
: Turn a numeric vector into a character vector formatted as absolute numbers. Useful for printing numerical data inrmd
orrnw
-reports.format_rel()
: Same asformat_abs()
, but for printing decimal numbers with a specific number of digits after the decimal mark.
calc_date()
: Calculate a vector holding date objects from a day, month and year number.calc_difftime_days()
: Calculate a diff vector holding the number of days between two vectors with date objects as entries.calc_difftime_years()
: Calculate the diff vector holding the number of years (also including fractional years) between two vectors with date objects as entries. This function also takes leap years into account and can be used for calculating the age of a person. Only for persons born on the 29th of february there is an exception, and their birth is mapped to the 1st of march in order to be able to compute their age.is_leap_year()
: Returns a logical answer to the question if a specific year is a leap year.date_2_year()
: Extracts a numeric vector holding year numbers from a vector holding date objects.
create_d_vars()
: Calculate absolute and relative difference vectors for one or multiple data.frame variables. E.g. if we have timevariable like ayear
a measurement variable like the population countn_pop
. Then functioncreate_d_vars()
can be used in order to calculate the following variables:d_pop
: Holding the absolute yearly growth of the populationn_pop
.da_pop
: Holding the relative yearly growth of the populationn_pop
create_d_vars_all()
: Likecreate_d_vars()
, but using all columns except of specific set, defined by the user.
lappli()
: Same aslapply()
, but also passing the variables.i
and.n
holding the item index and the item name to the given functionfunction(x, ..., .i, .n) {...}
.sappli()
: Same assapply()
, but also passing the variables.i
and.n
holding the item index and the item name to the given functionfunction(x, ..., .i, .n) {...}
.add_list_items()
: A function that adds new list entries to a given list object.
init_text_builder()
: Initialize atext_builder
. This is a tool for incrementally adding text blocks. One use case can be creating large LaTeX code snippets in R.set_fn_defaults()
: Set the defaults for a given function. Can be useful for customizing various functions.get_defaults()
: Return all function arguments including their default values for a given function.
These functions help validating objects a user passed into your functions (useful for package development). These functions allow to specify various requirements on the objects and automatically throw meaningful error messages, in case of failure. These functions should be used together with the composerr) package. The following validations are available:
validate_vector()
: Check if an object is atomicvalidate_logical()
: Check if an object is a logical vectorvalidate_character()
: Check if an object is a character vectorvalidate_numeric()
: Check if an object is a numeric vectorvalidate_integer()
: Check if an object is an integer vectorvalidate_list()
: Check if an object is a list
ttable()
: Same astable()
, but settinguseNA = "always"
.parse_copied_range()
: With this function you can parse a pasted range (Excel, HTML etc.) into a data.frame. This function is useful when you want to quickly access some structured data without being bothered by file access code.