Package 'Dmisc'

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

Help Index


Assign values to specified names in an environment and unpack the values (reverse of ⁠%<...%⁠)

Description

Assign values to specified names in an environment and unpack the values (reverse of ⁠%<...%⁠)

Usage

values %...>% names

Arguments

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.

Value

NULL. The function performs assignments in the specified environment.

Examples

## Not run: 
list(1, 2) %...>% c("x", "y")

## End(Not run)

Unpack and assign values to specified names in an environment [Experimental]

Description

Unpack and assign values to specified names in an environment [Experimental]

Usage

names %<...% values

Arguments

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.

Value

NULL. The function performs assignments in the specified environment.

See Also

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.

Examples

## Not run: 
c("x", "y") %<...% list(1, 2)

## End(Not run)

Convert numeric variables to weighted factors by group [Experimental]

Description

This function converts a numeric column in a data frame to a factor variable, allowing for custom break points and grouping.

Usage

cut3(tbl, var_name, breaks, groups = NULL, bf_args = list(), .inf = FALSE, ...)

Arguments

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 ⁠See Also⁠ section for more details.

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 base::cut.

Value

A data frame identical to the input tbl, with var_name converted to a factor.

See Also

base::cut, for the underlying cut function used. vignette("cut3", package = "Dmisc"), for examples and extended usage.

Examples

datos <- data.frame(edad = seq(1:100))
dplyr::count(cut3(datos, "edad", 5), edad)

Cut3 by quantile [Experimental]

Description

Cut3 by quantile [Experimental]

Usage

cut3_quantile(tbl, var_name, .labels = NULL, .groups = NULL, .inf = TRUE, ...)

Arguments

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

Value

same as tbl input with var_name converted to factor by quantiles

Examples

## Not run: 
datos <- data.frame(edad = seq(1:100))
cut3_quantile(datos, "edad")

## End(Not run)

Create a summary table for a data frame [Experimental]

Description

This function generates a summary table for a data frame, containing summary statistics for each variable in the data frame.

Usage

describe(data, digits = 4, t = TRUE, flextable = FALSE, ft_args = list(), ...)

Arguments

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 flextable function if flextable = TRUE (default is an empty list)

...

Other arguments to pass to the summary function

Value

A table containing summary statistics for each variable in the data frame

Examples

# Using summary
summary(cars)

# Using describe
describe(cars)
describe(cars, flextable = TRUE)

Retrieve or Create a Pin

Description

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.

Usage

pin_get_or_create(.data, .board, .name, type = "csv", ...)

Arguments

.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 pins::pin_write.

Value

The version of the pin that was retrieved or created.

Examples

## 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 [Experimental]

Description

Multiple variables to unique date variable [Experimental]

Usage

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")
)

Arguments

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 date argument

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

Value

tbl a new data.frame with the compute variable

Examples

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)