sumpreserving.rounding: Sum Preserving Rounding

Description Usage Arguments Author(s) Examples

Description

This function implements sum preserving rounding. If the supplied data is a matrix, then the sum of all row entries is preserved.

Usage

1
sumpreserving.rounding(data, digits=0, preserve=TRUE)

Arguments

data

Vector or data frame

digits

Number of digits to be round

preserve

Should the sum be preserved?

Author(s)

Alexander Robitzsch

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#############################################################################
# EXAMPLE 1: 
#############################################################################

# define example data
data <- c( 1455 , 1261 , 1067 , 970 , 582 , 97 )
data <- 100 * data / sum(data) 

( x1 <- round( data ) )
sum(x1)
(x2 <- sumpreserving.rounding( data ) )
sum(x2)

  ##   > ( x1 <- round( data ) )
  ##   [1] 27 23 20 18 11  2
  ##   > sum(x1)
  ##   [1] 101
  ##   > (x2 <- sumpreserving.rounding( data ) )
  ##   [1] 27 23 20 18 10  2
  ##   > sum(x2)
  ##   [1] 100

#############################################################################
# EXAMPLE 2: 
#############################################################################

# matrix input
data <- rbind( data , data )
( x1 <- round( data ) )
rowSums(x1)
(x2 <- sumpreserving.rounding( data ) )
rowSums(x2)

#############################################################################
# EXAMPLE 3: 
#############################################################################

x2 <- c( 1.4 , 1.4 , 1.2 )
round(x2)
sumpreserving.rounding(x2)
  ##   > round(x2)
  ##   [1] 1 1 1
  ##   > sumpreserving.rounding(x2)
  ##   [1] 1 2 1


Search within the miceadds package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.