SampleToSum: Sample from a Specific Range with a Target Vector Sum

Description Usage Arguments Value Note Author(s) References Examples

View source: R/SampleToSum.R

Description

Takes a sample from a given range (for example, 1 to 100), and a specified resulting vector length (for example, 10), which add up to a specified value.

Usage

1
2
3
4
5
6
7
SampleToSum(
  Target = 100,
  VecLen = 10,
  InRange = 1:100,
  Tolerance = 2,
  showSum = TRUE
)

Arguments

Target

The value that the vector should sum to

VecLen

The required vector length

InRange

The input range that values can be sampled from

Tolerance

A "buffer" for the Target argument, allowing the resulting sum to be slightly higher or slightly lower than specified.

showSum

Logical. Should the resulting total be shown?

Value

A vector

Note

There is a good chance that with certain settings, this will be VERY SLOW!

Author(s)

Ananda Mahto

References

http://stackoverflow.com/a/14687223/1270695

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
set.seed(1)
SampleToSum()

SampleToSum()

SampleToSum(Tolerance = 0)

SampleToSum(Tolerance = 0)

set.seed(123)
## You'll have to wait a few seconds here
SampleToSum(Target = 1163, VecLen = 15, InRange = 50:150)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.