Title: | Kiernan Nicholls Miscellaneous |
---|---|
Description: | Quality of life functions for interactive programming. Shortcuts for common combinations of functions or different default arguments. Not to be used in production level scripts, but useful for exploring and quickly manipulating data for easy analysis. Also imports a variety of packages to facilitate the installation of those imported packages on the host machine. |
Authors: | Kiernan Nicholls [aut, cre, cph]
|
Maintainer: | Kiernan Nicholls <[email protected]> |
License: | GPL-3 |
Version: | 0.2.1 |
Built: | 2025-03-08 04:47:32 UTC |
Source: | https://github.com/k5cents/k5 |
%out%
is an inverted version of the infix %in%
operator.
x %out% table
x %out% table
x |
vector: the values to be matched. Long vectors are supported. |
table |
vector or |
%out%
is currently defined as
"%out%" <- function(x, table) match(x, table, nomatch = 0) == 0
logical; if x
is not present in table
c("A", "B", "3") %out% LETTERS
c("A", "B", "3") %out% LETTERS
Can perform one of three rough conversions:
For interval contracts (e.g., "220 - 229", "9% or more", etc.), convert the character strings to proper interval notation.
For contracts with multiple discrete outcomes (e.g., Candidate names), convert the character vector to simple factors.
For markets with a single binary question (e.g., "Will the Democrats have
a brokered convention in 2020?"), contracts returned are always "Yes" which
is converted to TRUE
.
contract_convert(x, decimal = FALSE)
contract_convert(x, decimal = FALSE)
x |
A character vector of contract names. |
decimal |
Should percentages be converted to decimals? |
A interval factor, unique factor, or logical vector.
Use clipr::write_clip()
to write the last value as a character vector to
the system clipboard.
copy_last(x = .Last.value)
copy_last(x = .Last.value)
x |
The object to view, usually left as base::.Last.value. |
The value of the internal evaluation of a top-level R expression is always
assigned to .Last.value
before further processing (e.g., printing).
The same .Last.value
as before copied, invisibly.
Find the length of the set of difference between x
and y
vectors.
count_diff(x, y, ignore.case = FALSE)
count_diff(x, y, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
ignore.case |
logical; if |
sum(x %out% y)
The number of unique values of x
not in y
.
Other counting wrappers:
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
# only unique values are checked count_diff(c("VT", "NH", "ZZ", "ZZ", "ME"), state.abb)
# only unique values are checked count_diff(c("VT", "NH", "ZZ", "ZZ", "ME"), state.abb)
Count the total values of x
that are %in%
the vector y
.
count_in(x, y, na.rm = TRUE, ignore.case = FALSE)
count_in(x, y, na.rm = TRUE, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
na.rm |
logical; Should |
ignore.case |
logical; if |
sum(x %out% y)
The sum of x
present in y
.
Other counting wrappers:
count_diff()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
count_in(c("VT", "NH", "ZZ", "ME"), state.abb)
count_in(c("VT", "NH", "ZZ", "ME"), state.abb)
Count the total values of x
that are NA
.
count_na(x)
count_na(x)
x |
A vector to check. |
sum(is.na(x))
The sum of x
that are NA
Other counting wrappers:
count_diff()
,
count_in()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
count_na(c("VT", "NH", NA, "ME"))
count_na(c("VT", "NH", NA, "ME"))
Count the total values of x
that are are %out%
of the vector y
.
count_out(x, y, na.rm = TRUE, ignore.case = FALSE)
count_out(x, y, na.rm = TRUE, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
na.rm |
logical; Should |
ignore.case |
logical; if |
sum(x %out% y)
The sum of x
absent in y
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
count_out(c("VT", "NH", "ZZ", "ME"), state.abb)
count_out(c("VT", "NH", "ZZ", "ME"), state.abb)
A wrapper around dplyr::count()
with sort
set to TRUE
by default and
the an additional column created by default containing the proportional
fraction each observation makes of the whole.
count2(x, ..., wt = NULL, sort = TRUE, prop = TRUE, sum = NULL) count_vec(x, sort = TRUE, prop = TRUE, sum = NULL)
count2(x, ..., wt = NULL, sort = TRUE, prop = TRUE, sum = NULL) count_vec(x, sort = TRUE, prop = TRUE, sum = NULL)
x |
A data frame. |
... |
Variables to group by. |
wt |
Frequency weights. |
sort |
If |
prop |
If |
sum |
Column to replace with a cumulative sum ( |
A tibble of element counts
count2(iris, Species)
count2(iris, Species)
The period of time since a system file was modified.
file_age(...)
file_age(...)
... |
Arguments passed to |
A Period class object.
file_age(system.file("README.md", package = "campfin"))
file_age(system.file("README.md", package = "campfin"))
Call the file
command line tool with option -i
.
file_encoding(path)
file_encoding(path)
path |
A local file path or glob to check. |
A tibble of file encoding.
A shortcut for dat %>% filter(str_detect(column, "\\d"))
.
filter_rx(dat, col, pattern, ...)
filter_rx(dat, col, pattern, ...)
dat |
A data frame with a character column to filter. |
col |
The column containing a character vector to input. |
pattern |
Pattern to look for.. |
... |
Additional arguments passed to |
A subset of rows from dat
.
GAA Team Abbreviations by Season and Team ID
gaa
gaa
A data frame with 74 rows and 3 variables:
The fantasy season integer
The team ID integer
The normalized owner abbreviation for that year
...
A function shortcut for accessing .Last.value.
last_value(x = .Last.value)
last_value(x = .Last.value)
x |
The object to return, usually left as base::.Last.value. |
The value of the internal evaluation of a top-level R expression is always assigned to .Last.value (in package:base) before further processing (e.g., printing).
The same .Last.value
as before viewing, invisibly.
Save and load packages from file
load_my_packages(path = NULL) save_my_packages(x = NULL, path = tempfile())
load_my_packages(path = NULL) save_my_packages(x = NULL, path = tempfile())
path |
The path to a text file containing one package per line. If
|
x |
A character vector of package names to save. If |
The list of packages, invisibly.
Set NA
for the values of x
that are %in%
the vector y
.
na_in(x, y, ignore.case = FALSE)
na_in(x, y, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
ignore.case |
logical; if |
The vector x
missing any values in y
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
na_in(c("VT", "NH", "ZZ", "ME"), state.abb) na_in(1:10, seq(1, 10, 2))
na_in(c("VT", "NH", "ZZ", "ME"), state.abb) na_in(1:10, seq(1, 10, 2))
Set NA
for the values of x
that are %out%
of the vector y
.
na_out(x, y, ignore.case = FALSE)
na_out(x, y, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
ignore.case |
logical; if |
The vector x
missing any values not in y
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
na_out(c("VT", "NH", "ZZ", "ME"), state.abb) na_out(1:10, seq(1, 10, 2))
na_out(c("VT", "NH", "ZZ", "ME"), state.abb) na_out(1:10, seq(1, 10, 2))
Set NA
for the values of x
that contain a single repeating character and
no other characters.
na_rep(x, n = 0)
na_rep(x, n = 0)
x |
A vector to check. |
n |
The minimum number times a character must repeat. If 0, the default,
then any string of one character will be replaced with |
Uses the regular expression "^(.)\\1+$"
.
The vector x
with NA
replacing repeating character values.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
na_rep(c("VT", "NH", "ZZ", "ME"))
na_rep(c("VT", "NH", "ZZ", "ME"))
Print up to the getOption("max.print")
and ask the user if they want to
print more than that. This is most useful when printing tibbles with more
than 10 rows but less than getOption("max.print")
.
print_all(x, ask = TRUE)
print_all(x, ask = TRUE)
x |
Object to print, typically a data frame or vector. |
ask |
If the length of |
The object x (invisibly)
Find the proportion of values of x
that are distinct.
prop_distinct(x)
prop_distinct(x)
x |
A vector to check. |
length(unique(x))/length(x)
The ratio of distinct values x
to total values of x
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
prop_distinct(c("VT", "VT", NA, "ME"))
prop_distinct(c("VT", "VT", NA, "ME"))
Find the proportion of values of x
that are %in%
the vector y
.
prop_in(x, y, na.rm = TRUE, ignore.case = FALSE)
prop_in(x, y, na.rm = TRUE, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
na.rm |
logical; Should |
ignore.case |
logical; if |
mean(x %in% y)
The proportion of x
present in y
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_na()
,
prop_out()
,
what_in()
,
what_out()
prop_in(c("VT", "NH", "ZZ", "ME"), state.abb)
prop_in(c("VT", "NH", "ZZ", "ME"), state.abb)
Find the proportion of values of x
that are NA
.
prop_na(x)
prop_na(x)
x |
A vector to check. |
mean(is.na(x))
The proportion of values of x
that are NA
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_out()
,
what_in()
,
what_out()
prop_na(c("VT", "NH", NA, "ME"))
prop_na(c("VT", "NH", NA, "ME"))
Find the proportion of values of x
that are %out%
of the vector y
.
prop_out(x, y, na.rm = TRUE, ignore.case = FALSE)
prop_out(x, y, na.rm = TRUE, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
na.rm |
logical; Should |
ignore.case |
logical; if |
mean(x %out% y)
The proportion of x
absent in y
.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
what_in()
,
what_out()
prop_out(c("VT", "NH", "ZZ", "ME"), state.abb)
prop_out(c("VT", "NH", "ZZ", "ME"), state.abb)
Use readr::read_delim()
on a string copied to the clipboard. Defaults to
tab separator like given when copying cells from spreadsheets.
read_delim_clip(delim = "\t", ...)
read_delim_clip(delim = "\t", ...)
delim |
Single character used to separate fields within a record. |
... |
Additional arguments passed to |
A data frame read from the clipboard.
Use readr::read_delim()
without specifying any column types. All columns
are treated as character strings.
read_delim_dumb(file, delim = c(",", "\t", "|"), ...) read_csv_dumb(file, ...) read_tsv_dumb(file, ...)
read_delim_dumb(file, delim = c(",", "\t", "|"), ...) read_csv_dumb(file, ...) read_tsv_dumb(file, ...)
file |
Either a path to a file, a connection, or literal data. |
delim |
Single character used to separate fields within a record. |
... |
Additional arguments passed to |
A tibble data frame read from the file.
Apply either count_na()
or dplyr::n_distinct()
to every column of a data
frame and return the count and share of total values (either proportion
missing or proportion distinct).
var_missing(df) var_distinct(df)
var_missing(df) var_distinct(df)
df |
A data frame to glimpse. |
Invisibly, a table of statistics by column of a data frame.
var_missing(dplyr::storms) var_distinct(dplyr::storms)
var_missing(dplyr::storms) var_distinct(dplyr::storms)
Take an XML document object, write to an HTML file, and open in Firefox.
view_firefox(html)
view_firefox(html)
html |
An object which has the class |
The html object, invisibly.
Invoke a spreadsheet-style data viewer on a matrix-like R object. In a non-interactive session, the object is returned invisibly and nothing is "viewed".
view_last(x = .Last.value)
view_last(x = .Last.value)
x |
The object to view, usually left as base::.Last.value. |
The value of the internal evaluation of a top-level R expression is always
assigned to .Last.value
before further processing (e.g., printing).
The same .Last.value
as before viewing, invisibly.
Return the values of x
that are %in%
of the vector y
.
what_in(x, y, ignore.case = FALSE)
what_in(x, y, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
ignore.case |
logical; if |
x[which(x %in% y)]
The elements of x
that are %in%
y.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_out()
what_in(c("VT", "DC", NA), state.abb)
what_in(c("VT", "DC", NA), state.abb)
Return the values of x
that are %out%
of the vector y
.
what_out(x, y, na.rm = TRUE, ignore.case = FALSE)
what_out(x, y, na.rm = TRUE, ignore.case = FALSE)
x |
A vector to check. |
y |
A vector to compare against. |
na.rm |
logical; Should |
ignore.case |
logical; if |
x[which(x %out% y)]
The elements of x
that are %out%
y.
Other counting wrappers:
count_diff()
,
count_in()
,
count_na()
,
count_out()
,
na_in()
,
na_out()
,
na_rep()
,
prop_distinct()
,
prop_in()
,
prop_na()
,
prop_out()
,
what_in()
what_out(c("VT", "DC", NA), state.abb)
what_out(c("VT", "DC", NA), state.abb)
Invoke system tool to print newline, word, and byte counts for each file.
word_count(path, count = "")
word_count(path, count = "")
path |
Character vector of file paths. |
count |
The type of element to count, see details. |
One of five options or an empty string (default):
"lines" for newline characters (separating lines).
"words" for words separated by white space.
"chars" for individual characters.
"bytes" for total bytes, differs with multibyte characters.
"max" for the maximum display width of longest line.
A data frame of counts by file.
Use readr::format_delim()
on a data frame to copy a string to the
clipboard. Defaults to tab separator like given when copying cells from
spreadsheets.
write_delim_clip(x, delim = "\t", ...)
write_delim_clip(x, delim = "\t", ...)
x |
A data frame to write to clipboard. |
delim |
Single character used to separate fields within a record. |
... |
Additional arguments passed to |
Invisibly, the input data frame.
The value of the internal evaluation of a top-level R expression is always
assigned to .Last.value
before further processing (e.g., printing).
write_last(file = tempfile(), x = .Last.value, ...) save_last(file = tempfile(), x = .Last.value, ...)
write_last(file = tempfile(), x = .Last.value, ...) save_last(file = tempfile(), x = .Last.value, ...)
file |
File or connection to write to. |
x |
The object to write, usually left as base::.Last.value. |
... |
Additional arguments passed to the writing function (see Details). |
Four types of files are written, based on object class:
For data frames, a tab-separated file via readr::write_tsv()
.
For vectors, a newline-separated file via readr::write_lines()
.
For ggplots, a raster image (by default) via ggplot2::ggsave()
.
For other objects, an uncompressed data file via readr::write_rds()
.
The created file path, invisibly.