fstpackage / synthetic
1
#  syntheticbench - R package for benchmarking of dataset serialization
2
#
3
#  Copyright (C) 2019-present, Mark AJ Klik
4
#
5
#  This file is part of the lazyvec R package.
6
#
7
#  The lazyvec R package is free software: you can redistribute it and/or modify it
8
#  under the terms of the GNU Affero General Public License version 3 as
9
#  published by the Free Software Foundation.
10
#
11
#  The lazyvec R package is distributed in the hope that it will be useful, but
12
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
#  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
14
#  for more details.
15
#
16
#  You should have received a copy of the GNU Affero General Public License along
17
#  with the lazyvec R package. If not, see <http://www.gnu.org/licenses/>.
18
#
19
#  You can contact the author at:
20
#  - syntheticbench R package source repository : https://github.com/fstpackage/syntheticbench
21

22

23
#' Generate a logical vector with certain distribution of TRUE, FALSE and NA
24
#'
25
#' @param length length of the vector
26
#' @param true_false_na_ratio relative ratio of values TRUE, FALSE and NA
27
#'
28
#' @return logical vector
29
#' @export
30
sample_logical <- function(length, true_false_na_ratio = c(7, 3, 0)) {
31

32
  # normalize
33 2
  true_false_na_ratio <- true_false_na_ratio / sum(true_false_na_ratio)
34

35 2
  choice_set <- c(TRUE, FALSE, NA)[true_false_na_ratio > 0]
36 2
  true_false_na_ratio <- true_false_na_ratio[true_false_na_ratio > 0]
37

38 2
  sample(choice_set, length, prob = true_false_na_ratio, replace = TRUE)
39
}

Read our documentation on viewing source code .

Loading