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.20 C  0.78017713
#> 2  -0.85 C -1.92317778
#> 3   1.60 C -0.27364949
#> 4  -2.50 B  0.60371773
#> 5   1.50 B -1.31734831
#> 6   1.30 A  0.95558863
#> 7   0.34 B  0.01285354
#> 8  -0.32 B  0.29143609
#> 9  -0.95 A -2.01680556
#> 10  0.12 A -1.59648840
summary(df)
#>        x          y           z          
#>  Min.   :-2.500   A:3   Min.   :-2.0168  
#>  1st Qu.:-0.925   B:4   1st Qu.:-1.5267  
#>  Median :-0.100   C:3   Median :-0.1304  
#>  Mean   :-0.096         Mean   :-0.4484  
#>  3rd Qu.: 1.060         3rd Qu.: 0.5256  
#>  Max.   : 1.600         Max.   : 0.9556  

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   0.790 A  0.7600
#> 2  -0.056 C  0.5600
#> 3   2.800 A -0.1800
#> 4   0.400 B -0.0026
#> 5   2.700 A  0.5900
#> 6   1.100 B  0.1800
#> 7   1.800 A  1.4000
#> 8   1.300 A  0.8300
#> 9  -0.720 C  0.5500
#> 10 -0.260 A  0.5500
summary(df)
#>        x                y                   z          
#>  Min.   :-0.7200   Length:10          Min.   :-0.1800  
#>  1st Qu.: 0.0580   Class :character   1st Qu.: 0.2725  
#>  Median : 0.9450   Mode  :character   Median : 0.5550  
#>  Mean   : 0.9854                      Mean   : 0.5237  
#>  3rd Qu.: 1.6750                      3rd Qu.: 0.7175  
#>  Max.   : 2.8000                      Max.   : 1.4000