NiceNumbERRR-vignette.Rmd
library(NiceNumbERRR)
#> Loading required package: tidyverse
#> ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
#> ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
#> ✓ tibble 3.0.4 ✓ dplyr 1.0.2
#> ✓ tidyr 1.1.2 ✓ stringr 1.4.0
#> ✓ readr 1.4.0 ✓ forcats 0.5.0
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag() masks stats::lag()
This package provides basic functions that make numbers display nicely. In most real-world problems, the datasets are raw and we need to deal with number formats to make them readable for humans or for computers. Usually, a few or more lines of coding are needed while dealing with number-display problems so we are thinking of compressing the time and programming work on this issue. This package solves this kind of problems in a way of transfer forward and backward from long digit numbers to human-readable ones. There are functions doing single number transactions, column transactions from a data frame, and displaying colors of input numbers.
number | human | filesize_human | filesize_bytes |
---|---|---|---|
12000 | 12K | 22.6MB | 1200000 |
123998 | $123M | 123KB | 12366667 |
8800000 | $133.6556B | 8.889GB | 1888889 |
Let’s try these functions one by one.
to_human
This function converts numeric value to human-readable string representations. Users need to use a specific number as input and choose decimal precision and prefixes of filesize or numbers as optionals. The function will return a human-readable string.
Let’s convert each value from number column in toy dataframe to human-readable strings:
for (i in 1:length(df$number)){
print(to_human(df$number[i]))
}
#> [1] "12K"
#> [1] "124K"
#> [1] "9M"
Then let’s try converting filesizes:
to_numeric
How about changing from human-readable string back to numeric values?
This function converts a human-readable value to a Python readable numeric value. Users need to use a specific human-readable string of numbers as input and choose the prefixes of filesize or numbers as optionals. The function will return a float.
We can try the human column in toy dataframe:
for (i in 1:length(df$human)){
print(to_numeric(df$human[i]))
}
#> [1] 12000
#> [1] 1.23e+08
#> [1] 133655600000
Try filesize_human column as well:
for (i in 1:length(df$filesize_human)){
print(to_numeric(df$filesize_human[i], family = "filesize"))
}
#> [1] 22600000
#> [1] 123000
#> [1] 8.889e+09
to_df
We have two functions to_human
and to_numeric
to convert single values. But changing the whole column just using one function will be much convenient. Here it comes:
This function changes the formatting of text in one or more columns of data in a dataframe. The inputs should include a data frame, column name(s), and two optionals: transform type(eg. human) and type of prefixes. The function will return a dataframe with the values from the input columns transferred to the transform type(human-readable by default).
Let’s try convert two columns from toy dataframe:
to_color
Besides converting methods introduced above, here comes another function to display number nicer.
This function separate numeric values to parts starting from the right and each part contains three digits. Then it gives different colors to each part and the default colors are red, green, yellow, and blue. Users need to use a specific number as input and choose a list of colors they want to assign on the number as an optional. The function will return a string that can be used in print() function to visual numbers with colors in the terminal.
Let’s give it a try:
to_color(1234567L, c("red", "green", "yellow", "blue"))
#> [1] "\033[32m1\033[0m\033[33m2\033[0m\033[34m3\033[0m\033[31m4\033[0m\033[32m5\033[0m\033[33m6\033[0m\033[34m7\033[0m"
The combination of numbers, slashes and letters represents the color for that number. These colored strings cannot display in R for now, but works in terminal.