Reputation: 33
I have multiple functions following this schematic:
function1 <- function(){
string_samples <- c("string1", "string2", "string3", "string4", "string5")
sample(string_samples, 1)
}
function1 gets called in another function as follows:
function_process <- function(){
a <- sample(1:3,1)
a2 <- sample(1:3,1)
g <- gsub(" ", "", paste(function1(), "-", sample(1:a,1)))
g2 <- gsub(" ", "", paste(function1(), "-", sample(1:a2,1)))
rnd_g <- sample(1:2, 1)
if(rnd_g==1) x <- paste(g)
else if(rnd_g==2) x <- paste(g, g2)
return(x)
}
I want to avoid that one string value is getting picked multiple times at return(x)
. How could I achieve this in a simple way?
Right now results could be something like: string1-1 string1-2
which I want to avoid.
Upvotes: 2
Views: 44
Reputation: 173898
This seems like a strange way to do things, but the following simplified function will achieve it:
function_process <- function(){
a <- sample(1:3, 1)
a2 <- sample(1:3, 1)
g <- function1()
g2 <- function1()
while(g == g2) {
g2 <- function1()
}
g <- paste0(g, "-", sample(1:a,1))
g2 <- paste0(g2, "-", sample(1:a2, 1))
if(sample(0:1, 1)) return(g)
paste(g, g2)
}
Note that you can use paste0
instead of using gsub
to remove the spaces that paste
adds by default.
You can see the same string is never used twice here:
set.seed(1)
for(i in 1:10) print(function_process())
#> [1] "string1-1"
#> [1] "string3-1"
#> [1] "string1-1"
#> [1] "string2-1"
#> [1] "string2-2 string4-2"
#> [1] "string4-1"
#> [1] "string2-2"
#> [1] "string3-1 string4-3"
#> [1] "string1-1"
#> [1] "string4-1"
Created on 2022-01-31 by the reprex package (v2.0.1)
Upvotes: 1