Sample integer number from given continuous vector of line lengths and probabilities, with total n

n_sample_length(n, l_lengths, weights)

Arguments

n

Sum of integer values returned

l_lengths

Numeric vector of line lengths

weights

Relative probabilities of samples on lines

See also

Examples

n <- 10 l_lengths <- 1:5 weights <- 9:5 (res <- n_sample_length(n, l_lengths, weights))
#> [1] 1 2 2 2 3
sum(res)
#> [1] 10
n <- 100 l_lengths <- c(12, 22, 15, 14) weights <- c(38, 10, 44, 34) (res <- n_sample_length(n, l_lengths, weights))
#> [1] 26 12 36 26
sum(res)
#> [1] 100
# more examples: n_sample_length(5, 1:5, c(0.1, 0.9, 0, 0, 0))
#> [1] 0 5 0 0 0
n_sample_length(5, 1:5, c(0.5, 0.3, 0.1, 0, 0))
#> [1] 2 2 1 0 0
l <- flowlines[2:6, ] l_lengths <- line_length(l)
#> Transforming to CRS +proj=aeqd +lat_0=53.82473675 +lon_0=-1.52515262 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
n <- n_sample_length(10, l_lengths, weights = l$All)