If I have a vector of strings of any length say;
vec <- c("a","b","c","d","e","f","g","h","i")
I am looking for a function to sample n number of values from this vector using a strategy shown below. Showing figures since it's hard to explain.
function call:
result:
schematic diagram:
fn(vector=vec,n=1)
"a"
|
a b c d e f g h i
fn(vector=vec,n=2)
"a" "i"
_______________
| |
a b c d e f g h i
fn(vector=vec,n=3)
"a" "e" "i"
_______________
| | |
a b c d e f g h i
fn(vector=vec,n=4)
"a" "c" "g" "i"
_______________
| | | |
a b c d e f g h i
fn(vector=vec,n=5)
"a" "c" "e" "g" "i"
_______________
| | | | |
a b c d e f g h i
The sampling does not have to be accurate. The values can be from roughly the correct region but must be consistent. The string vector can be even or odd.
One way would be to use seq()
, taking advantage of it's length.out=
argument to get the evenly spaced indices that you seek:
fn <- function(x, n) {
x[round(seq(1,length(x), length.out=n))]
}
## Check that it works
fn(vec, 1)
# [1] "a"
fn(vec, 2)
# [1] "a" "i"
fn(vec, 4)
# [1] "a" "d" "f" "i"
fn(vec, 8)
# [1] "a" "b" "c" "d" "f" "g" "h" "i"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With