Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sampling a vector by equal split

Tags:

r

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.

like image 305
rmf Avatar asked May 18 '16 19:05

rmf


1 Answers

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"
like image 194
Josh O'Brien Avatar answered Oct 02 '22 20:10

Josh O'Brien