for循环中的R

编程入门 行业动态 更新时间:2024-10-25 03:24:44
for循环中的R - sample()会产生相同的排列吗?(R - sample() within for loop generates identical permutations?)

当我运行一个简单的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.

最满意答案

对于可重现的示例,只需将options[1]替换为常量集,并将labels替换为内置或自定义数据框。 (顺便说一下,伟大的变量名称都不是基本函数。)只要查看for循环的内部部分,就可以for除data.frame的第一列之外的所有变量进行洗牌。 这可以按预期工作。 在完成perm后放入print(names(perm)) ,你会看到。 然后,您将此数据框转换为之前的结果。 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.

更多推荐

本文发布于:2023-07-29 15:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!