Format columns in a data.frame.

formatTableColumns(tbl, columns, signifDigits, maxLevels = 10)

Arguments

tbl

A data.frame or similar object.

columns

Character vector giving the column names of tbl that should be formatted.

signifDigits

Numeric scalar, the number of significant digits to round numeric columns to. Set to NULL to skip rounding.

maxLevels

Numeric scalar. If character columns have at most this number of unique values, they will be encoded as factors.

Value

The input data.frame with formatted columns.

Author

Charlotte Soneson

Examples

df <- formatTableColumns(data.frame(x = rnorm(10),
                                    y = sample(LETTERS[seq_len(3)], 10,
                                               replace = TRUE),
                                    z = rnorm(10)),
                         columns = c("x", "y"),
                         signifDigits = 2, maxLevels = 10)
df
#>         x y          z
#> 1   1.300 C  1.0840904
#> 2  -0.530 B  1.0288958
#> 3   1.200 C  0.1586200
#> 4   0.830 A  1.2212023
#> 5   1.200 C -0.6702936
#> 6   0.410 B  0.2470124
#> 7  -0.056 A -1.7228651
#> 8   0.780 A  0.8318224
#> 9  -1.900 C -1.4629965
#> 10 -0.270 B -0.1268366
summary(df)
#>        x           y           z           
#>  Min.   :-1.9000   A:3   Min.   :-1.72287  
#>  1st Qu.:-0.2165   B:3   1st Qu.:-0.53443  
#>  Median : 0.5950   C:4   Median : 0.20282  
#>  Mean   : 0.2964         Mean   : 0.05887  
#>  3rd Qu.: 1.1075         3rd Qu.: 0.97963  
#>  Max.   : 1.3000         Max.   : 1.22120  

df <- formatTableColumns(data.frame(x = rnorm(10),
                                    y = sample(LETTERS[seq_len(3)], 10,
                                               replace = TRUE),
                                    z = rnorm(10)),
                         columns = c("x", "y", "z"),
                         signifDigits = 2, maxLevels = 2)
df
#>         x y     z
#> 1   1.400 C  1.80
#> 2   1.900 A -0.67
#> 3  -0.670 B -0.19
#> 4   0.760 B -0.76
#> 5   1.600 C -1.00
#> 6   0.440 B  1.00
#> 7  -0.051 C  0.52
#> 8  -0.110 A -0.67
#> 9  -0.270 A -0.99
#> 10 -0.560 C -0.69
summary(df)
#>        x                y                   z          
#>  Min.   :-0.6700   Length:10          Min.   :-1.0000  
#>  1st Qu.:-0.2300   Class :character   1st Qu.:-0.7425  
#>  Median : 0.1945   Mode  :character   Median :-0.6700  
#>  Mean   : 0.4439                      Mean   :-0.1650  
#>  3rd Qu.: 1.2400                      3rd Qu.: 0.3425  
#>  Max.   : 1.9000                      Max.   : 1.8000