Description Usage Arguments Value Note See Also Examples
With only one line, change the precision of different data vectors in a data.table. One group is casted into integer, and one group is rounded to the given precision.
1 2 3 |
DT |
data.table. Data object wich has at least one numeric colum. |
integerList |
list of integers refering to columns' id or name. These columns are casted to integer – and NOT rounded. |
decimalList |
list of integers refering to columns' id or name. These columns are rounded to the given precision. |
decimal |
integer. Set precision for vectors in decimalList. Default is |
latex |
logic. Add math sections to decimalList items (e.g.: |
... |
Dots. An arbitrary number and variety of arguments, passing arguments on to other functions. |
data.table. The same data.table as iput with certain numeric values rounded.
This function has side effect and changes the input data.frame.
Other Utils: KH.changeDecimal
,
KH.ifNull
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | set.seed(1000)
library(data.table)
d <- data.table(
group = sample(LETTERS[1:3], 10, replace = TRUE),
integer = rnorm(10, 5, 1),
decimal = rnorm(10, 5, 1)
)
d
# group integer decimal
# 1: A 4.614511 5.170057
# 2: C 4.524132 5.155079
# 3: A 5.719751 5.024932
# 4: C 4.981494 2.953415
# 5: B 3.626882 5.213154
# 6: A 4.017572 7.670072
# 7: C 4.445511 3.772984
# 8: B 5.121381 5.834247
# 9: A 4.879128 5.532572
#10: A 3.663959 4.353175
d1 <- KH.roundAll(copy(d), integerList = c(2), decimalList = c(3)) # default precision is presumably 2
d1
# group integer decimal
# 1: A 4 5.17
# 2: C 4 5.16
# 3: A 5 5.02
# 4: C 4 2.95
# 5: B 3 5.21
# 6: A 4 7.67
# 7: C 4 3.77
# 8: B 5 5.83
# 9: A 4 5.53
#10: A 3 4.35
d1 <- KH.roundAll(copy(d), integerList = c(2), decimalList = c(3), decimal = 3)
d1
# group integer decimal
# 1: A 4 5.170
# 2: C 4 5.155
# 3: A 5 5.025
# 4: C 4 2.953
# 5: B 3 5.213
# 6: A 4 7.670
# 7: C 4 3.773
# 8: B 5 5.834
# 9: A 4 5.533
#10: A 3 4.353
d1 <- KH.roundAll(copy(d), integerList = c('integer', 'decimal'))
d1
# group integer decimal
# 1: A 4 5
# 2: C 4 5
# 3: A 5 5
# 4: C 4 2
# 5: B 3 5
# 6: A 4 7
# 7: C 4 3
# 8: B 5 5
# 9: A 4 5
#10: A 3 4
KH.roundAll(d, decimalList = c('integer', 'decimal'), decimal = 3)
d
# group integer decimal
# 1: A 4.615 5.170
# 2: C 4.524 5.155
# 3: A 5.720 5.025
# 4: C 4.981 2.953
# 5: B 3.627 5.213
# 6: A 4.018 7.670
# 7: C 4.446 3.773
# 8: B 5.121 5.834
# 9: A 4.879 5.533
#10: A 3.664 4.353
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.