Skip to contents

Reformats (numeric) variables in data frames or tibbles to use mark as the thousand separator, rounded to a specified number of digits. It is designed to preceed a call to kable.

Usage

table_format(x, ...)

# S3 method for class 'tbl'
table_format(x, names, digits = rep(0, length(names)), mark = ",", ...)

# S3 method for class 'data.frame'
table_format(x, names, digits = rep(0, length(names)), mark = ",", ...)

Arguments

x

a data frame or tibble with variables to reformat.

...

other arguments parsed to methods (currently unused)

names

character vector with variable names in x to reformat.

digits

the number of decimal places to round variables (using round). digits can be: length 1, and applied to all variables in names; or, length equal to length(names), with decimal places specific to each variable in names.

mark

character used to separate thousands. Defaults to mark = ",".

Value

an object of class x, where variables in names have been reformatted. Note that variables in names will have been coerced to characters.

Examples

dat <- data.frame(a = LETTERS[1:10], b = (1:10) * 500, c = (-5:4) * 500)
table_format(dat, c("b", "c"), digits = 1)
#> New names:
#>  `` -> `...1`
#>  `` -> `...2`
#> New names:
#>  `` -> `...1`
#>  `` -> `...2`
#>    a       b        c
#> 1  A   500.0 -2,500.0
#> 2  B 1,000.0 -2,000.0
#> 3  C 1,500.0 -1,500.0
#> 4  D 2,000.0 -1,000.0
#> 5  E 2,500.0   -500.0
#> 6  F 3,000.0      0.0
#> 7  G 3,500.0    500.0
#> 8  H 4,000.0  1,000.0
#> 9  I 4,500.0  1,500.0
#> 10 J 5,000.0  2,000.0
table_format(dat, c("b", "c"), digits = c(1, 2))
#> New names:
#>  `` -> `...1`
#>  `` -> `...2`
#> New names:
#>  `` -> `...1`
#>  `` -> `...2`
#>    a       b         c
#> 1  A   500.0 -2,500.00
#> 2  B 1,000.0 -2,000.00
#> 3  C 1,500.0 -1,500.00
#> 4  D 2,000.0 -1,000.00
#> 5  E 2,500.0   -500.00
#> 6  F 3,000.0      0.00
#> 7  G 3,500.0    500.00
#> 8  H 4,000.0  1,000.00
#> 9  I 4,500.0  1,500.00
#> 10 J 5,000.0  2,000.00