r - Get an uniform distributed sample but keep the order
问题描述
How could i get a sample of a values of a vector but keep the order without compairing the values themself against each other?
for example:
V1 contains values (1,2,3,4,5,6,7,8,9,10,11,12,13,14)
I woule like to get a sample
sample <- (2,7,10,14)
As you can see the values are still on order but randomly selected.
But if i use a function sample or rdunif in R I get random orderd selection:
ie. (7,10,2,14)
Thank you!
解决方案
With the following solution you do not compare the elements of your original vector in order to sort them; the only thing you do is shuffling a vector of logical values (TRUE
or FALSE
).
Let's say you want to pick n
elements from the already-ordered vector v
and maintain their order. Then you can do
v <- 1:14
n <- 4
set.seed(42) # for reproducibility
logi <- sample(c(rep(TRUE, n), rep(FALSE, length(v) - n)))
v[logi]
# [1] 1 6 7 14
EDIT to prove that the vector v
can be any vector, and we still manage to maintain its original order.
set.seed(1)
n <- 4
v <- sample(14, replace = FALSE)
v
# [1] 9 4 7 1 2 12 3 6 10 8 5 11 13 14
set.seed(42) # for reproducibility
logi <- sample(c(rep(TRUE, n), rep(FALSE, length(v) - n)))
v[logi]
# [1] 9 12 3 14
These numbers respect indeed the original order of vector v
.
推荐阅读
- asp.net - 如何使用 asp.net MVC 为 DropDownListFor 中的每个条目添加一个字符串?
- asp.net-core - ASP.NET Core HealthChecks 使用 RabbitMQ 报告假阴性
- android - 是否可以将 Java 字符串常量传递给 Gradle 文件?
- php - 如何检查是否仅选中了一个或两个复选框
- redis - 是否可以在 rejson 数组中使用 LRANGE?
- ios - iOS 模拟器在运行排毒测试时冻结(挂起)
- sap-cloud-platform - SAP Cloud Platform Portal 服务中如何实现动态数据?
- scala - Spark Scala java.lang.NoSuchMethodError:在 CDH 5.16 中执行 fat jar 时
- ios - 如何为 UIViews 的“中心”属性设置动画?
- laravel-nova - Laravel Nova 在产品创建/更新页面上按模式添加类别