どうも、ちょげ(@chogetarou)です。
ベクトルの要素をランダムに取得する方法を紹介します。
方法

ベクトルの要素をランダムに抽出するには、sample()を使います。
まず、sample()を呼び出します。
そして、sample()の第1引数に対象のベクトル、第2引数に抽出する個数を指定します。
#vect=対象のベクトル, count=抽出する個数
result <- sample(vect, count)
上記のsample()は、ベクトル(vector)の要素をランダムに取得します。
使用例
numbers <- c(1, 2 ,3, 4, 5)
result <- sample(numbers, 1)
result2 <- sample(numbers, 5)
print(result)
print(result2)
出力:
> print(result)
[1] 3
> print(result2)
[1] 1 5 3 4 2
コメント