Consider two vectors :
R> l
[1] "a" "b" "c" "d" "e" "f"
R> s
[1] "b" "d" "f"
I cannot hard-code the indexes to be removed from the sampling.
How can I sample elements from l
that are not present in s
?
Consider two vectors :
R> l
[1] "a" "b" "c" "d" "e" "f"
R> s
[1] "b" "d" "f"
I cannot hard-code the indexes to be removed from the sampling.
How can I sample elements from l
that are not present in s
?
you can try this
l <-c("a","b","c","d","e","f")
s <- c("b", "d", "f")
l2 <- l[!l %in% s] # elements present in "l" and not in "s"
sample(l2, 10, replace = TRUE)
I just found it using this post
sample(l[-c(match(s, l))])
PS : Sorry, for asking before searching thoroughly.
EDIT :-
For the vectors :
R> l <- c(1:5000)
R> s <- c(100:1100)
I ran the micobenchmark :
R> microbenchmark(func(l, s), sample(l[-c(match(s, l))], 10), times=1000L)
Here, func() is defined as follows :
R> func <- function(l, s) {
l2 <- l[!l %in% s] # elements present in "l" and not in "s"
return(sample(l2, 10, replace = TRUE))
}
The microbenchmark returned:
Unit: microseconds
expr min lq mean median uq max neval cld
func(l, s) 218.7 221.3 234.1 222.1 229.5 2937 1000 a
sample(l[-c(match(s, l))], 10) 222.5 226.9 238.8 227.8 235.7 2933 1000 a
I guess, their performance is quite comparable.