简体   繁体   English

在R中遇到嵌套for循环的问题

[英]Having an issue with nested for loops in R

I am currently using apTreeshape to simulate phylogenetic trees using the "Yule-Hardy" Method. 我目前正在使用apTreeshape使用“Yule-Hardy”方法模拟系统发育树。 What I want to do is randomly generate between 20 and 25 different numbers for three different groupings (small, medium and large trees) and then generate about 40 trees for every random number chosen from within the grouping. 我想要做的是为三个不同的分组(小型,中型和大型树)随机生成20到25个不同的数字,然后为从分组中选择的每个随机数生成大约40棵树。

I know how I would do this in Python of Matlab, but in R things seem to behave a bit differently. 我知道如何在Python的Python中做到这一点,但在R中,事情看起来有点不同。

My thought was that if I were to generate a vector full of random numbers (one for each size grouping) and then use that to generate a vector which would basically contain all of the repeated values of each random number. 我的想法是,如果我要生成一个充满随机数的向量(每个大小分组一个),然后使用它生成一个基本上包含每个随机数的所有重复值的向量。

Here is what I have: 这是我有的:

sm_leaves<-c(sample(3:50,25,replace=F));
s_leafy<-numeric();

for (i in 1:length(sm_leaves)) { 
    for (j in 1:10) {
        s_leafy[j+i-1]=sm_leaves[i];
    }
}

This is giving me output like: 这给我的输出如下:

> s_leafy
[1]  5 38  6 22 29 20 19 46  9 18 39 50 34 11 43  7  8 32 10 42 14 37
[23] 23 13 28 28 28 28 28 28 28 28 28 28

But What I want is something more like: 但我想要的更像是:

> s_leafy
[1]  5  5  5  5  5  5  5  5  5  5 38 38 38 38 38 38 38 38 38 ... 28 28 28 28 28 28 28 28 28 28

My reason for doing this is merely so that I can append this vector to a data frame along with all of the randomly generated trees - I need 2000 of them, so doing this by hand ain't quite practical. 我这样做的原因仅仅是我可以将这个向量与所有随机生成的树一起附加到数据框架 - 我需要2000个,所以手动执行这个操作并不太实际。

All I have really been able to deduce from my previous attempts to solve this problem is that generally speaking while loops should be used instead of for loops, and many people have talked about using expand.grid, but I don't think that the latter is particularly useful in this case. 从我以前尝试解决这个问题的所有我真正能够推断的是,一般来说,应该使用循环而不是for循环,很多人都谈到使用expand.grid,但我不认为后者在这种情况下特别有用。

Thanks for reading, I hope my problem isn't too trivial (although I wouldn't be surprised if it were). 感谢阅读,我希望我的问题不是太微不足道(虽然我不会感到惊讶)。

Using 'rep' is clearly the answer for how to do this quickly in R, but why doesn't your code work? 使用'rep'显然是如何在R中快速完成此任务的答案,但为什么你的代码不起作用? A little investigation reveals why. 一点调查揭示了原因。

First, take out the randomness and give yourself a simple, reproducible example. 首先,取出随机性并给自己一个简单,可重复的例子。 Set sm_leaves to c(3,4,5) and see what happens. 将sm_leaves设置为c(3,4,5),看看会发生什么。 You get: 你得到:

3 4 5 5 5 5 5 5 5 5 5 5 3 4 5 5 5 5 5 5 5 5 5 5

which still looks wrong. 哪个看起来还不错。 You expected ten 3s, ten 4s, ten 5s right? 你预计10个3s,10个4s,10个5s吧? Hmmm. 嗯。 Add a print statement to your loop to see where the values are being stuck: 在循环中添加一个print语句,以查看值被卡住的位置:

> for (i in 1:length(sm_leaves)) { 
   for (j in 1:10) {
    print(j+i-1)
    s_leafy[j+i-1]=sm_leaves[i];
   }
 }

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
...etc....

Oops. 哎呀。 Your vector index is wrong. 你的矢量索引是错误的。 j+i-1 is jumping back after every inner loop and overwriting the earlier values. j + i-1在每个内循环后跳回并覆盖先前的值。 You want: 你要:

s_leafy[j + (i - 1)*10]=sm_leaves[i];

So maybe this is just a simple case of you missing the *10 in the expression! 所以也许这只是一个简单的例子,你错过了表达式中的* 10!

Note however that a lot of simple vector manipulation is best done using R's functions such as rep, and seq, and "[", as explained in the other answers here. 但请注意,很多简单的向量操作最好使用R的函数,如rep,seq和“[”,如此处的其他答案中所述。

Apologies if I don't quite understand the question, but what about: 如果我不太明白这个问题,请道歉,但是:

sm_leaves <- sample(3:50, 25, replace=FALSE)
s_leafy <- rep(sm_leaves, each=10)

You want rep() with the each=10 option: 你想要rep()each=10选项:

> set.seed(42)   
> sm_leaves <- sample(3:50,25,replace=F) 
> s_leafy <- rep(sm_leaves, each=3)        ## here rep=3 to generate small sample
> s_leafy   
 [1] 46 46 46 47 47 47 16 16 16 40 40 40 31 31 31 25 25 
[18] 25 33 33 33  8  8  8 29 29 29 30 30 30 20 20 20 42  
[35] 42 42 36 36 36 11 11 11 18 18 18 34 34 34 35 35 35 
[52]  6  6  6 17 17 17 19 19 19 28 28 28 44 44 44 41 41 
[69] 41 26 26 26  4  4  4   
>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM