R - for 循环中的sample() 生成相同的排列?

发布于 2024-12-12 15:39:45 字数 1134 浏览 0 评论 0原文

当我运行一个简单的 for 循环来计算向量的 X 个排列时,sample() 函数为每次迭代返回相同的排列。

下面是我的代码:

options <- commandArgs(trailingOnly=T)
labels <- read.table(options[2], header=F)
holder <- c()

for (i in 1:options[1]){

    perm <- sample(labels[,2:ncol(labels)], replace=F)
    perm <- cbind(as.character(labels[1]), perm)
    holder <- rbind(holder, perm)

}

write.table(holder, file=options[3], row.names=F, col.names=F, quote=F, sep='\t')

这是有原因的吗?是否有另一种简单的方法来生成向量的 1000 个排列?

*在评论后添加 - 一个可复制的示例*

vec <- 1:10
holder <-c()
for (i in 1:5){
    perm <- sample(vec, replace=F)
    holder <- rbind(holder, perm)
}

> holder
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
perm    3    2    1   10    9    6    7    4    5     8
perm    5    8    2    3    4   10    9    1    6     7
perm   10    7    3    1    4    2    5    8    9     6
perm    9    5    2    8    3    1    6   10    7     4
perm    3    7    5    6    8    2    1    9   10     4

这工作正常!我想我某个地方有错误!我的输入可能很混乱。

谢谢, D、

谢谢, D .

When I run a simple for loop to compute X number of permutations of a vector, the sample() function returns the same permutation for each iteration.

Below is my code:

options <- commandArgs(trailingOnly=T)
labels <- read.table(options[2], header=F)
holder <- c()

for (i in 1:options[1]){

    perm <- sample(labels[,2:ncol(labels)], replace=F)
    perm <- cbind(as.character(labels[1]), perm)
    holder <- rbind(holder, perm)

}

write.table(holder, file=options[3], row.names=F, col.names=F, quote=F, sep='\t')

Is there a reason why this is so? Is there another simple way to generate say 1000 permutations of a vector?

*Added after comment - a replicable example*

vec <- 1:10
holder <-c()
for (i in 1:5){
    perm <- sample(vec, replace=F)
    holder <- rbind(holder, perm)
}

> holder
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
perm    3    2    1   10    9    6    7    4    5     8
perm    5    8    2    3    4   10    9    1    6     7
perm   10    7    3    1    4    2    5    8    9     6
perm    9    5    2    8    3    1    6   10    7     4
perm    3    7    5    6    8    2    1    9   10     4

And this works fine! I guess I have a bug somewhere! My input is perhaps in a mess.

Thanks,
D.

Thanks,
D.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-12-19 15:39:45

对于可重现的示例,只需将 options[1] 替换为常量集,将 labels 替换为内置或自指定的数据框。 (顺便说一句,伟大的变量名都不是基本函数。)只需查看 for 循环的内部部分,即可对 data.frame 中除第一列之外的所有列进行洗牌。这正如你所期望的那样工作。完成perm后,将print(names(perm))放入,你就会看到。然后,您将此数据框rbind到之前的结果。 rbind 认识到它正在处理数据帧,有助于重新调整不同数据帧的列顺序,以便列名称对齐(这通常是您希望它执行的操作;名称列的定义了它是哪一列,并且您希望适当地扩展每一列。)

问题是您正在对数据框的列进行排列,而不是像您想象的那样对“向量”进行排列。

For a reproducible example, just replace options[1] with a constant set and labels to a built-in or self-specified data frame. (By the way, neither are great variable names being base functions.) Just looking at the inner part of your for loop, you shuffle all but the first column of a data.frame. This works as you expect. Put print(names(perm)) in after finishing making perm and you will see. You then rbind this data frame to the previous results. rbind, recognizing it is working with data frames, helpfully reshuffles the column order of the different data frames so that the column names line up (which, generally, is what you would want it to do; the name of the column defines which one it is and you would want to extend each column appropriately.)

The problem is that you are doing permutations on columns of a data frame, not "of a vector" as you seem to think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文