Title: | Daniel miscellaneous functions |
---|---|
Description: | The Dmisc package provides a collection of versatile R functions developed by Daniel E. de la Rosa. These are general-purpose tools, not tied to any specific domain, aiming to aid a wide range of tasks in data analysis and visualization. |
Authors: | Daniel E. de la Rosa [aut, cre] |
Maintainer: | Daniel E. de la Rosa <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.11 |
Built: | 2024-11-20 06:07:29 UTC |
Source: | https://github.com/dnldelarosa/Dmisc |
%<...%
)Assign values to specified names in an environment and unpack the values (reverse of %<...%
)
values %...>% names
values %...>% names
values |
A list or vector containing the values to be assigned to the names. |
names |
A list or character vector specifying the names of variables to be assigned. |
NULL. The function performs assignments in the specified environment.
## Not run: list(1, 2) %...>% c("x", "y") ## End(Not run)
## Not run: list(1, 2) %...>% c("x", "y") ## End(Not run)
Unpack and assign values to specified names in an environment
names %<...% values
names %<...% values
names |
A list or character vector specifying the names of variables to be assigned. |
values |
A list or vector containing the values to be assigned to the names. |
NULL. The function performs assignments in the specified environment.
Additional arguments that can be passed to names include
.envirThe environment where the variables will be assigned. Defaults to the global environment.
.warnLogical flag indicating whether to show a warning message. Defaults to TRUE.
## Not run: c("x", "y") %<...% list(1, 2) ## End(Not run)
## Not run: c("x", "y") %<...% list(1, 2) ## End(Not run)
This function converts a numeric column in a data frame to a factor variable, allowing for custom break points and grouping.
cut3(tbl, var_name, breaks, groups = NULL, bf_args = list(), .inf = FALSE, ...)
cut3(tbl, var_name, breaks, groups = NULL, bf_args = list(), .inf = FALSE, ...)
tbl |
data.frame: The data frame containing the data to be converted. |
var_name |
character: The name of the numeric variable to convert to a factor. |
breaks |
numeric: Break points defining factor levels. See |
groups |
character: The name of the variable for grouping data before conversion. |
bf_args |
list: Additional arguments to be passed to the break function. |
.inf |
logical: Whether to extend break points with -Inf and Inf. |
... |
Additional arguments passed to |
A data frame identical to the input tbl
, with var_name
converted to a factor.
base::cut
, for the underlying cut function used.
vignette("cut3", package = "Dmisc")
, for examples and extended usage.
datos <- data.frame(edad = seq(1:100)) dplyr::count(cut3(datos, "edad", 5), edad)
datos <- data.frame(edad = seq(1:100)) dplyr::count(cut3(datos, "edad", 5), edad)
cut3_quantile(tbl, var_name, .labels = NULL, .groups = NULL, .inf = TRUE, ...)
cut3_quantile(tbl, var_name, .labels = NULL, .groups = NULL, .inf = TRUE, ...)
tbl |
data.frame: Database connection or data.frame |
var_name |
character: variable name |
.labels |
list: labels for the breaks |
.groups |
character: name of a groups variable |
.inf |
logical: indicates if the breaks need to be extended by -Inf and Inf |
... |
argument passed to quantile |
same as tbl
input with var_name
converted to factor by
quantiles
## Not run: datos <- data.frame(edad = seq(1:100)) cut3_quantile(datos, "edad") ## End(Not run)
## Not run: datos <- data.frame(edad = seq(1:100)) cut3_quantile(datos, "edad") ## End(Not run)
This function generates a summary table for a data frame, containing summary statistics for each variable in the data frame.
describe(data, digits = 4, t = TRUE, flextable = FALSE, ft_args = list(), ...)
describe(data, digits = 4, t = TRUE, flextable = FALSE, ft_args = list(), ...)
data |
A data frame containing the data to be summarized |
digits |
The number of 0digits to display in the summary table (default is 4) |
t |
If TRUE, the table will be transposed (default is TRUE) |
flextable |
If TRUE, the table will be converted to a simple flextable (default is FALSE) |
ft_args |
A list of additional arguments to pass to the |
... |
Other arguments to pass to the |
A table containing summary statistics for each variable in the data frame
# Using summary summary(cars) # Using describe describe(cars) describe(cars, flextable = TRUE)
# Using summary summary(cars) # Using describe describe(cars) describe(cars, flextable = TRUE)
This function attempts to retrieve a specified pin from a board. If the pin doesn't exist or the data is different, it creates a new pin.
pin_get_or_create(.data, .board, .name, type = "csv", ...)
pin_get_or_create(.data, .board, .name, type = "csv", ...)
.data |
The data to be pinned if a new pin is to be created. |
.board |
The board where the pin is located or should be created. |
.name |
The name of the pin to be retrieved or created. |
type |
The type of data being pinned, defaults to 'csv'. |
... |
Additional arguments to be passed to |
The version of the pin that was retrieved or created.
## Not run: board <- pins::board_register_local() data <- mtcars pin_version <- pin_get_or_create(data, board, "mtcars_pin") ## End(Not run)
## Not run: board <- pins::board_register_local() data <- mtcars pin_version <- pin_get_or_create(data, board, "mtcars_pin") ## End(Not run)
Multiple variables to unique date variable
vars_to_date( tbl, year = NULL, quarter = NULL, month = NULL, day = NULL, date = NULL, drop_vars = TRUE, clean_names = FALSE, date_format = "%d-%m-%y", origin = "1900-01-01", .round = c("end", "middle", "start") )
vars_to_date( tbl, year = NULL, quarter = NULL, month = NULL, day = NULL, date = NULL, drop_vars = TRUE, clean_names = FALSE, date_format = "%d-%m-%y", origin = "1900-01-01", .round = c("end", "middle", "start") )
tbl |
data.frame or tbl connection |
year |
year variable position or name |
quarter |
quarter variable position or name |
month |
month variable position or name |
day |
day variable position or name |
date |
a variable name or position containing a like date format |
drop_vars |
indicates if variables should be dropped |
clean_names |
indicates if all variable names should be cleaned |
date_format |
actual date format of variable in |
origin |
base date for variable convertion to date |
.round |
indicates if the date should be rounded to the end, middle or start of the period |
tbl a new data.frame with the compute variable
tbl <- data.frame( year = rep("2021", 12), month = month.name, day = sample(1:3, 12, TRUE), value = sample(100:1000, 12, TRUE) ) tbl vars_to_date(tbl, year = 1, month = 2, day = 3) # and supports various frequencies and date formats tbl <- data.frame( year = rep("2021", 12), quarter = sample( c( "Enero-Marzo", "Abril-Junio", "Julio-Septiembre", "Octubre-Diciembre" ), 12, TRUE ), value = sample(100:1000, 12, TRUE) ) tbl vars_to_date(tbl, year = 1, quarter = 2)
tbl <- data.frame( year = rep("2021", 12), month = month.name, day = sample(1:3, 12, TRUE), value = sample(100:1000, 12, TRUE) ) tbl vars_to_date(tbl, year = 1, month = 2, day = 3) # and supports various frequencies and date formats tbl <- data.frame( year = rep("2021", 12), quarter = sample( c( "Enero-Marzo", "Abril-Junio", "Julio-Septiembre", "Octubre-Diciembre" ), 12, TRUE ), value = sample(100:1000, 12, TRUE) ) tbl vars_to_date(tbl, year = 1, quarter = 2)