round_numerics: Round all numeric columns in a data frame to a given number...

Description Usage Arguments Value Author(s) See Also Examples

View source: R/round_numerics.R

Description

Takes a data frame and looks at each column. If the column is _not_ a numeric, it will be returned unchanged. If the column is a numeric, it will be replaced with a numeric rounded to the number of decimal places specified in the argument. The function rounds all numerics except those named in the "no_round" argument. This is a adapted from the pre-existing function freeze_factor_levels().

Usage

1
round_numerics(x, digits = 12, no_round = NULL)

Arguments

x

a data frame.

digits

numeric number of decimal places to which to round.

no_round

character vector. Columns of "x" named in this vector will be returned unchanged.

Value

data frame of the same size, names, and types as "x". Numerics may have changed.

Author(s)

Bill Forrest forrest@gene.com

See Also

factor

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
 myDat <- data.frame(
             animal = factor( c('cat', 'dog', 'cat', 'bird', 'fish', 'dog', 'cat'),
                              levels = c('bird','cat','dog','fish')
                            ),
             covering = factor( c('hair','hair', 'hair', 'feathers', 'scales', 'hair', 'hair'),
                                levels = c('feathers','hair','scales')
             ),
             size = c(4.12, 4.123, 4.1234, 4.12345, 4.123456, 4.1234567, 4.12345678 ),
             mass = c(9.12, 9.123, 9.1234, 9.12345, 9.123456, 9.1234567, 9.12345678 )
          ) # end of data.frame 'myDat'

print( myDat )

### round numeric columns of a data.frame
myNewDat <- round_numerics( myDat, digits = 3 )
print( myNewDat )

### Round one but not the other.
myNewDat2 <- round_numerics( myDat, digits = 3, no_round = 'mass' )
print( myNewDat2 )

### Round to a different number of digits
myNewDat3 <- round_numerics( myDat, digits = 4, no_round = c('mass') )
print( myNewDat3 )

wfforrest/maeve documentation built on Jan. 1, 2021, 12:47 p.m.