Randomly distribute specific String variables into vector

Is there any function that randomly distributes specific strings in vector form?
As an example Str <- ('A','B','C','D','E')
and I want to make Str100 which contains 100 randomly distributed variables from Str
like ('A','B','A','D','C','E', .......... )
What function is needed for this?
Also is it possible to make it with same number of each variables in Str like 20 'A' and 20 'B' etc.

set.seed(123)


# Without replacement -----------------------------------------------------

# get letters A to E
(vec <- LETTERS[1:5])
#> [1] "A" "B" "C" "D" "E"

# sample from that vector 100 times
(str100 <- sample(vec, 100, replace = TRUE))
#>   [1] "C" "C" "B" "B" "C" "E" "D" "A" "B" "C" "E" "C" "C" "A" "D" "A" "A" "E"
#>  [19] "C" "B" "B" "A" "C" "D" "A" "C" "E" "D" "B" "E" "A" "A" "B" "C" "D" "E"
#>  [37] "E" "C" "A" "B" "E" "E" "D" "E" "B" "A" "A" "C" "A" "E" "A" "B" "D" "D"
#>  [55] "C" "A" "B" "A" "B" "D" "E" "E" "C" "A" "D" "A" "A" "C" "D" "A" "C" "E"
#>  [73] "C" "B" "E" "E" "C" "B" "B" "B" "D" "B" "B" "D" "D" "A" "C" "C" "A" "C"
#>  [91] "E" "B" "C" "B" "E" "E" "C" "D" "D" "D"

# unequal output
dplyr::count(dplyr::tibble(x = str100), x)
#> # A tibble: 5 × 2
#>   x         n
#>   <chr> <int>
#> 1 A        21
#> 2 B        20
#> 3 C        23
#> 4 D        17
#> 5 E        19


# With replacement --------------------------------------------------------

# get letters A to E, and repeat all 20 times
(vec <- LETTERS[1:5] |> rep(20))
#>   [1] "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C"
#>  [19] "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A"
#>  [37] "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D"
#>  [55] "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B"
#>  [73] "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E"
#>  [91] "A" "B" "C" "D" "E" "A" "B" "C" "D" "E"

# sample from that vector 100 times, without replacement
(str100 <- sample(vec, 100, replace = FALSE))
#>   [1] "C" "D" "D" "D" "E" "D" "A" "C" "B" "E" "A" "C" "C" "A" "E" "E" "A" "B"
#>  [19] "E" "C" "E" "B" "A" "B" "B" "D" "B" "D" "A" "E" "C" "A" "A" "D" "D" "B"
#>  [37] "C" "C" "E" "A" "D" "C" "D" "C" "C" "A" "D" "E" "E" "E" "C" "E" "B" "A"
#>  [55] "C" "D" "B" "A" "A" "D" "D" "C" "B" "B" "C" "B" "B" "C" "C" "E" "A" "B"
#>  [73] "D" "B" "B" "E" "D" "E" "E" "A" "B" "A" "B" "E" "D" "D" "C" "C" "E" "D"
#>  [91] "E" "E" "D" "A" "B" "A" "A" "A" "C" "B"

# equal output
dplyr::count(dplyr::tibble(x = str100), x)
#> # A tibble: 5 × 2
#>   x         n
#>   <chr> <int>
#> 1 A        20
#> 2 B        20
#> 3 C        20
#> 4 D        20
#> 5 E        20

Created on 2022-07-02 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.