f_data: Convert and Abbreviate Units of Data.

Description Usage Arguments Value Examples

View source: R/f_data.R

Description

Convert numeric data to shorter form with unit abbreviations attached. For example, move from 10,000,000,000 (Bytes) to 10GB (Gigabytes) instead.

f_byte - Force the abbreviation to bytes unit (B).

f_kilo - Force the abbreviation to kilobytes unit (KB).

f_mega - Force the abbreviation to megabytes unit (MB).

f_giga - Force the abbreviation to gigabytes unit (GB).

f_tera - Force the abbreviation to terabytes unit (TB).

f_peta - Force the abbreviation to petabytes unit (PB).

f_exa - Force the abbreviation to exabytes unit (EB).

f_zetta - Force the abbreviation to zettabytes unit (ZB).

f_yotta - Force the abbreviation to yottabytes unit (YB).

Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
f_data(
  x,
  binary = FALSE,
  digits = 0,
  pad.char = " ",
  less.than.replace = FALSE,
  sep = "",
  mix.units = FALSE,
  from = "B",
  ...
)

ff_data(...)

f_byte(
  x,
  to = "B",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_byte(...)

f_kilo(
  x,
  to = "KB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_kilo(...)

f_mega(
  x,
  to = "MB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_mega(...)

f_giga(
  x,
  to = "GB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_giga(...)

f_tera(
  x,
  to = "TB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_tera(...)

f_peta(
  x,
  to = "PB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_peta(...)

f_exa(
  x,
  to = "EB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_exa(...)

f_zetta(
  x,
  to = "ZB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_zetta(...)

f_yotta(
  x,
  to = "YB",
  binary = FALSE,
  digits = 0,
  suffix = f_data_abbreviation(to),
  pad.char = " ",
  less.than.replace = FALSE,
  from = "B",
  sep = "",
  ...
)

ff_yotta(...)

Arguments

x

A vector of data units.

binary

logical. If TRUE the result uses binary conversion, otherwise decimal conversion is used. See https://en.wikipedia.org/wiki/Binary_prefix for additional information on standards.

digits

The number of digits to round to. .

pad.char

A character to use for leading padding if lengths of output are unequal. Use NA to forgo padding.

less.than.replace

logical. If TRUE values lower than lowest place value will be replaced with a less than sign followed by the integer representation of the place value. For example, if "0GB" then replacement will be "<1GB".

sep

The separator to use between the number and data unit abbreviation.

mix.units

logical. If TRUE then units can be mixed. Typically, this is not a good idea for the sake of comparison. It is most useful when there is a total row which is a sum of the column and this value's unit exceeds the unit of the rest of the column.

from

The starting unit. Typically, this is assumed to be 'Bytes' ('B'). Must be one of c("Bit", "Byte", "Kilobyte", "Megabyte", "Gigabyte", "Terabyte", "Petabyte", "Exabyte", "Zettabyte", "Yottabyte") or c("b", "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"). These are case sensitive.

to

The units to convert to. See the from parameter for accepted units.

suffix

A suffix to use for the units at the end of the numeric string. Typically the user will not interact with this argument. Meant for internal modularity of functions.

...

ignored.

Value

Returns a converted and abbreviated vector of units of data.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 
x <- c(NA, '3', '-', -233456789, -2334567890, 10^(0:10))
f_data(x)
f_data(x, pad.char = NA)
f_data(x, mix.units = TRUE)
f_data(x, mix.units = TRUE, binary = TRUE)
f_data(x, mix.units = TRUE, binary = TRUE, digits = 2)
f_byte(100000000, from = 'GB', binary = TRUE)
f_giga(10000000000)
f_giga(10000000000, suffix = 'Gb')

library(tidyverse)
set.seed(15)
dat <- data_frame(
    bytes = round(rnorm(7, 1e7, 7.95e6), 0),
    days = constant_weekdays %>%
        as_factor()
)

dat %>%
    mutate(
        data = f_data(bytes, less.than.replace = TRUE),
        weekday = f_weekday(days, distinct = TRUE) %>%
            as_factor()
    )

dat %>%
    mutate(days = days %>% as_factor()) %>%
    ggplot(aes(days, bytes, group = 1)) +
        geom_line() +
        geom_point() +
        scale_y_continuous(labels = f_data) +
        scale_x_discrete(labels = ff_weekday(distinct = TRUE))

## End(Not run)

numform documentation built on Oct. 10, 2021, 1:10 a.m.