| Title: | Some utils for working with R |
|---|---|
| Description: | Providing functions that may be of common use. |
| Authors: | Guillaume Noblet [aut, cre] |
| Maintainer: | Guillaume Noblet <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-10-09 10:16:12 UTC |
| Source: | https://github.com/impact-initiatives/impactR.utils |
Abord bad argument
abort_bad_argument(arg1, must, not = NULL, arg2 = NULL, same = NULL)abort_bad_argument(arg1, must, not = NULL, arg2 = NULL, same = NULL)
arg1 |
An argument |
must |
What arg must be |
not |
Optional. What arg must not be. |
arg2 |
Another argument |
same |
The same type as arg1 |
A stop statement
Count missing values for vars (by group)
count_missing_values(df, vars, group = NULL, pivot = TRUE)count_missing_values(df, vars, group = NULL, pivot = TRUE)
df |
A data frame. |
vars |
Variable names. |
group |
Grouping column names. Default to NULL. |
pivot |
Logical. If TRUE, return a pivoted data frame. |
Date to day and month
date_to_dm(vec)date_to_dm(vec)
vec |
A vector of type POSIXct. |
The same vector with only the day and month in the env locale.
Remove columns from a data frame
deselect(df, ...)deselect(df, ...)
df |
A data frame. |
... |
Column names. |
A tibble with columns removed
Get colnames that start, end, or both with a string
df_col_start(df, pattern_start) df_col_end(df, pattern_end) df_col_start_end(df, pattern_start, pattern_end)df_col_start(df, pattern_start) df_col_end(df, pattern_end) df_col_start_end(df, pattern_start, pattern_end)
df |
A data frame. |
pattern_start |
A start string |
pattern_end |
An end string. |
A vector of column names
Remove columns from a data frame that exists in another tibble
df_diff(df_a, df_b, ...)df_diff(df_a, df_b, ...)
df_a |
A data frame to remove columns from. |
df_b |
A data frame to extract columns names from. |
... |
Columns from df_b and df_a to keep in data frame. |
A tibble with some columns removed.
Stop statement "If not in colnames" with colnames
if_not_in_stop(df, cols, df_name, arg = NULL)if_not_in_stop(df, cols, df_name, arg = NULL)
df |
A dataframe |
cols |
A vector of column names (quoted) |
df_name |
Provide the tibble name as a character string |
arg |
Default to NULL. |
A stop statement
Import a dataset from a CSV or a XLSX file. It ensures that the right type of column is guessed using the max value of guess_max by default.
import_csv(path, delim = ",", clean_names = TRUE, guess_max = 21474836) import_xlsx(path, sheet = 1, clean_names = TRUE, guess_max = 21474836) import_full_xlsx(path = NULL, clean_names = TRUE, guess_max = 21474836)import_csv(path, delim = ",", clean_names = TRUE, guess_max = 21474836) import_xlsx(path, sheet = 1, clean_names = TRUE, guess_max = 21474836) import_full_xlsx(path = NULL, clean_names = TRUE, guess_max = 21474836)
path |
The path to the file. |
delim |
Field separator, default is ",". |
clean_names |
Boolean. If TRUE, uses |
guess_max |
Maximum guess of types. Default to 21474836. |
sheet |
A character string of the sheet name or the sheet position |
A dataframe.
import_csv(): Import a .csv file.
import_xlsx(): Import one sheet from a .xlsx.
import_full_xlsx(): Import all sheets from a .xlsx.
Left join a list of dataframes
left_join_dup(df_a, df_b, ..., duplicated_columns = 3)left_join_dup(df_a, df_b, ..., duplicated_columns = 3)
df_a |
A dataframe. |
df_b |
Another dataframe. |
... |
Columns to join by. |
duplicated_columns |
Strategy between 1 and 4. Default to 3. See section "Treating duplicated columns". |
A left-joined data frame.
There are 4 ways implemented on treating columns with the same names in both datasets:
1: an usual left-join with suffix being empty for df_a and ".dup_col" for df_b;
2: removing all duplicated columns from df_b, keeping only those of df_a;
3: remove the column from df_b if all values from df_b that are not NAs equal the values from df_a;
4: remove the column from df_b if all values from df_b are identical to values from df_a.
left_joints() is a simple reducing functions based on dplyr::left_join(). left_joints_dup() is a reduce wrapper around left_join_dup().
left_joints(list, ...) left_joints_dup(list, ..., duplicated_columns = 3)left_joints(list, ...) left_joints_dup(list, ..., duplicated_columns = 3)
list |
A list of data frames. |
... |
Columns to join by. |
duplicated_columns |
Strategy between 1 and 4. Default to 3. See section "Treating duplicated columns". |
A left-joined data frame.
Mutate with replacement if NULL or NA values
mutate_if_nulla(df, col, replacement)mutate_if_nulla(df, col, replacement)
df |
A data frame. |
col |
A column of |
replacement |
A replacement of the same type as |
A mutated data frame.
Named group split
named_group_split(df, group)named_group_split(df, group)
df |
A data frame. |
group |
Quoted column name to group by. |
A split and named list of data frames.
recode_values() recodes a singular value. recode_na() recodes usual values of NAs, for instance 999, NULL, NaN, and NA.
recode_values(df, values, to_value, ...) recode_na(df, ...)recode_values(df, values, to_value, ...) recode_na(df, ...)
df |
A data frame. |
values |
A vector of values. |
to_value |
A value. |
... |
Column names. |
If the column type is a character and the replacement a numeric, then the numeric is coerced to a character. If the column type is a numeric and the replacement is a character, then the column is coerced to character. NAs will remains NAs of the right type.
A data frame with recoded values to one value.
Rename columns
rename_cols(df, old_cols, new_cols)rename_cols(df, old_cols, new_cols)
df |
A data frame. |
old_cols |
A vector of old quoted variable names. |
new_cols |
A vector of new quoted variable names. |
Updated tibble with new names
Row-wise optimum (default to pmax) of several columns
row_optimum( df, ..., optimum = "max", max_name = "pmax", min_name = "pmin", na_rm = TRUE, keep = "all" )row_optimum( df, ..., optimum = "max", max_name = "pmax", min_name = "pmin", na_rm = TRUE, keep = "all" )
df |
A data frame. |
... |
Numeric column names. |
optimum |
Should we calculate "max", "min" or "both"? Default to TRUE. |
max_name |
Column name for the mutated pmax. |
min_name |
Column name for the mutated pmin. |
na_rm |
Remove NAs. Default to TRUE. |
keep |
To be used within mutate. Default to "all". |
A data frame with pmax, pmin or both (and all columns or none, depending on "keep")
row_pattern_count() count row-wise occurences of a pattern over the data frame; row_na_count() count row-wise number of NAs over the data frame.
row_string_count(df, pattern = "", new_colname = "count") row_na_count(df, new_colname = "count")row_string_count(df, pattern = "", new_colname = "count") row_na_count(df, new_colname = "count")
df |
A data frame. |
pattern |
A pattern to pass to |
new_colname |
The newly-mutated column name. Default to "count". |
A mutated data frame.
subvec_in() gets a subset of a vector if vector elements are in a given set. subvec_not_in() get a subset of a vector if vector elements are NOT in a fiven set.
subvec_in(vector, set) subvec_not_in(vector, set)subvec_in(vector, set) subvec_not_in(vector, set)
vector |
A vector to subset. |
set |
A set-vector. |
A subset of a list or a vector.