formattedcut: Convert continuous vector variable to formatted factors

View source: R/formattedcut.R

formattedcutR Documentation

Convert continuous vector variable to formatted factors

Description

Often, when a continuous data is converted to factors using the ⁠base R⁠ cut function, the resultant ⁠Class Interval⁠ column provide data with scientific notation which normally appears confusing to interpret, especially to casual data scientist. This function provide a more user-friendly output and is provided in a formatted manner. It is a easy to implement function.

Usage

formattedcut(data, breaks, cut = FALSE)

Arguments

data

A vector of the data to be converted to factors if not cut already or the vector of a cut data

breaks

Number of classes to break the data into

cut

Logical to indicate if the cut function has already being applied to the data, defaults to FALSE.

Value

The function returns a ⁠data frame⁠ with three or four columns i.e ⁠Lower class⁠, ⁠Upper class⁠, ⁠Class interval⁠ and Frequency (if the cut is FALSE).

Cut

The ⁠data frame⁠

Examples

DD <- rnorm(100000)
formattedcut(DD, 12, FALSE)

DD1 <- cut(DD, 12)
DDK <- formattedcut(DD1, 12, TRUE)
DDK
# if data is not from a data frame, the frequency distribution is required.
as.data.frame(DDK %>%
group_by(`Lower class`, `Upper class`, `Class interval`) %>%
tally())

JobNmadu/Dyn4cast documentation built on March 5, 2025, 9:56 p.m.