This function returns a table with numeric columns rounded accordingly the specified number of decimal digits. Numeric columns are coverted to character after rounding.

table_format(table, digits)

Arguments

table

a data frame.

digits

a vector with non negative integer values with length equals the number of columns of table. These number represent the number of digits used to round numeric values. The number is ignored when the correponding column isn't numeric. NA and negative values are ignored.

Value

a table with column rounded to the number of decimal digits specified. All columns are coverted to character, so numeric functions can not be direct applied to them anymore. Because of this, it is not recommended assign the result of the function to the object used as table argument.

See also

Examples

x <- table_format(head(rock), digits = c(1, 2, 3, 4)) x
#> area peri shape perm #> 1 4990.0 2791.90 0.090 6.3000 #> 2 7002.0 3892.60 0.149 6.3000 #> 3 7558.0 3930.66 0.183 6.3000 #> 4 7352.0 3869.32 0.117 6.3000 #> 5 7943.0 3948.54 0.122 17.1000 #> 6 7979.0 4010.15 0.167 17.1000
str(x)
#> 'data.frame': 6 obs. of 4 variables: #> $ area : chr "4990.0" "7002.0" "7558.0" "7352.0" ... #> $ peri : chr "2791.90" "3892.60" "3930.66" "3869.32" ... #> $ shape: chr "0.090" "0.149" "0.183" "0.117" ... #> $ perm : chr "6.3000" "6.3000" "6.3000" "6.3000" ...
x <- table_format(head(iris), c(2, 3, 2, 3, NA)) x
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.10 3.500 1.40 0.200 setosa #> 2 4.90 3.000 1.40 0.200 setosa #> 3 4.70 3.200 1.30 0.200 setosa #> 4 4.60 3.100 1.50 0.200 setosa #> 5 5.00 3.600 1.40 0.200 setosa #> 6 5.40 3.900 1.70 0.400 setosa
str(x)
#> 'data.frame': 6 obs. of 5 variables: #> $ Sepal.Length: chr "5.10" "4.90" "4.70" "4.60" ... #> $ Sepal.Width : chr "3.500" "3.000" "3.200" "3.100" ... #> $ Petal.Length: chr "1.40" "1.40" "1.30" "1.50" ... #> $ Petal.Width : chr "0.200" "0.200" "0.200" "0.200" ... #> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1
x <- table_format(head(rock), c(-1, NA, 3, 4)) x
#> area peri shape perm #> 1 4990 2791.90 0.090 6.3000 #> 2 7002 3892.60 0.149 6.3000 #> 3 7558 3930.66 0.183 6.3000 #> 4 7352 3869.32 0.117 6.3000 #> 5 7943 3948.54 0.122 17.1000 #> 6 7979 4010.15 0.167 17.1000
str(x)
#> 'data.frame': 6 obs. of 4 variables: #> $ area : int 4990 7002 7558 7352 7943 7979 #> $ peri : num 2792 3893 3931 3869 3949 ... #> $ shape: chr "0.090" "0.149" "0.183" "0.117" ... #> $ perm : chr "6.3000" "6.3000" "6.3000" "6.3000" ...