重新排列向量的子集位置

huangapple go评论66阅读模式
英文:

Permute the position of a subset of a vector

问题

不要回答我要翻译的问题。

以下是已翻译的内容:

我想对向量的子集进行排列。

例如,假设我有一个向量(x),然后我选择向量的一个随机子集(例如,40% 的值)。

我想要的是生成一个新的向量(x2),与(x)相同,除了随机子集中的值的位置被随机交换。

例如:

  • x = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • 随机子集 = 1, 4, 5, 8
  • x2 可能是 = 4, 2, 3, 8, 1, 6, 7, 5, 9, 10

这是一个示例向量(x)以及如何选择一个占其值40%的随机子集的索引的方法。帮助生成(x2)将不胜感激!

x <- seq(1,10,1)
which(x%in%sample(x)[seq_len(length(x)*0.40)])
英文:

I want to permute a subset of a vector.

For example, say I have a vector (x) and I select a random subset of the vector (e.g., 40% of its values).

What I want to do is output a new vector (x2) that is identical to (x) except the positions of the values within the random subset are randomly swapped.

For example:

  • x = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • random subset = 1, 4, 5, 8
  • x2 could be = 4, 2, 3, 8, 1, 6, 7, 5, 9, 10

Here's an an example vector (x) and how I'd select the indices of a random subset of 40% of its values. Any help making (x2) would be appreciated!

x &lt;- seq(1,10,1)
which(x%in%sample(x)[seq_len(length(x)*0.40)])

答案1

得分: 3

首先从索引中抽取比例为 psample,然后对具有该索引的元素进行采样和重新分配。

f <- function(x, p=0.4) {
  r <- sample(seq_along(x), length(x)*p)
  x[r] <- sample(x[r])
  `attr<-`(x, 'subs', r)  ## 添加具有被抽样的索引的属性
}

set.seed(42)
f(x)
# [1]  8  2  3  4  1  5  7 10  6  9
# attr(,"subs")
# [1]  1  5 10  8

数据:

x <- 1:10
英文:

First draw a sample of proportion p from the indices, then sample and re-assign elements with that indices.

f &lt;- \(x, p=0.4) {
  r &lt;- sample(seq_along(x), length(x)*p)
  x[r] &lt;- sample(x[r])
  `attr&lt;-`(x, &#39;subs&#39;, r)  ## add attribute w/ indices that were sampled
}

set.seed(42)
f(x)
#  [1]  8  2  3  4  1  5  7 10  6  9
# attr(,&quot;subs&quot;)
# [1]  1  5 10  8

Data:

x &lt;- 1:10

答案2

得分: 1

以下是翻译好的代码部分:

# 确保有更快的代码来完成你的要求,但这是一个解决方案:
x <- seq(1,10,1)
y <-  which(x %in% sample(x)[seq_len(length(x) * 0.40)]) # 定义 "y" 为随机子集的向量

# 需要的库
library(combinat)

permutation <- permn(y) # permn() 函数在R中生成元素x的所有排列的列表。
# https://www.geeksforgeeks.org/calculate-combinations-and-permutations-in-r/

permutation_sampled <- sample(permutation, 1) # 从排列中随机抽取一个。

x[y] <- permutation_sampled[[1]] # 使用y作为应替代元素的索引,将所选排列替换到x中。

希望这对你有所帮助!如果你有任何其他问题,请随时提出。

英文:

For sure there is a faster code to do what you are asking, but, a solution would be:

x &lt;- seq(1,10,1)
y &lt;-  which(x%in%sample(x)[seq_len(length(x)*0.40)]) # Defined as &quot;y&quot; the vector of the random subset 

# required libraries
library(combinat)

permutation &lt;- permn(y) # permn() function in R generates a list of all permutations of the elements of x. 
# https://www.geeksforgeeks.org/calculate-combinations-and-permutations-in-r/

permutation_sampled &lt;- sample(permutation,1) # Sample one of the permutations.

x[y] &lt;- permutation_sampled[[1]] # Substitute the selected permutation in x using y as the index of the elements that should be substituted.

huangapple
  • 本文由 发表于 2023年2月18日 02:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定