解释R中的for循环 [英] Explaining a for loop in R
问题描述
我对R非常陌生,对R编程也很陌生.我有以下问题及其答案(这不是我的).我试图理解为什么某些值,从哪里获得的,为什么使用它们,等等.
I'm very new to R, and much more new to programming in R. I have the following question and its answer (which is not mine). I've trying to understand why some values, from where they are obtained, why they are used, etc.
问题:使向量3 5 7 9 11 13 15 17具有for循环.开始 用x = numeric()并用for循环填充此向量
Question: Make the vector 3 5 7 9 11 13 15 17 with a for loop. Start with x=numeric() and fill this vector with the for loop
我知道我必须创建x = numeric(),以便可以用从循环中获得的结果填充它.
I know I have to create x=numeric() so I can fill it with the result obtained from the loop.
同学的答案是:
> x <- numeric()
> for(i in 1:8){
if(i==1){ ## Why ==1 and not 0, or any other value
x[i] <- 3
}else{
x[i] <- x[i-1]+2 ### And why i-1
}
我在类似的问题中遇到类似的问题
I'm having similar problems in questions like:
进行for循环,将向量的第二个元素添加到第一个元素, 从结果中减去第三个元素,再次添加第四个元素,然后 这样整个向量的长度
Make a for loop that adds the second element of a vector to the first, subtracts the third element from the result, adds the fourth again and so on for the entire length of the vector
到目前为止,我已经创建了向量和空向量
So far, I created the vector and the empty vector
> y = c(5, 10, 15, 20, 25, 30)
> answer <- 0
然后,当我尝试执行for循环时,我被卡在这里:
And then, when I try to do the for loop, I get stuck here:
for(i in 1:length(y)){
if(i...){ ### ==1? ==0?
answer = y[i] ###and here I really don't know how to continue.
}else if()
}
当我告诉您时,请相信我,我已经在这里阅读了一些对问题的答复,例如
Believe me when I tell you I've read several replies to questions here, like in How to make a vector using a for loop, plus pages and pages about for loop, but cannot really figure how to solve these (and other) problems.
I repeat, I'm very new, so I'm struggling trying to understand it. Any help would be much appreciated.
推荐答案
首先,我将对循环进行注释,以回答循环的作用.
First, I will annotate the loop to answer what the loop is doing.
# Initialize the vector
x <- numeric()
for(i in 1:8){
# Initialize the first element of the vector, x[1]. Remember, R indexes start at 1, not 0.
if(i==1){
x[i] <- 3
} else {
# Define each additional element in terms of the previous one (x[i - 1]
# is the element of x before the current one.
x[i] <- x[i-1]+2 ### And why i-1
}
}
使用循环并扩大循环(如指令状态)的更好解决方案是这样的:
A better solution that uses a loop and grows it (like the instructions state) is something like this:
x <- numeric()
for(i in 1:8){
x[i] <- 2 * i + 1
}
这仍然不是处理问题的好方法,因为在循环内增长向量非常缓慢.要解决此问题,您可以通过告诉numeric
您想要的向量的长度来预分配向量:
This is still not a good way to do things because growing a vector inside a loop is very slow. To fix this, you can preallocate the vector by telling numeric
the length of the vector you want:
x <- numeric(8)
解决此问题的最佳方法是:
The best way to solve this would be:
2 * 1:8 + 1
使用矢量化操作.
为帮助您解决其他问题,建议将循环的每个步骤写成表格.例如,对于我的解决方案,该表将是
To help you solve your other problem, I suggest writing out each step of the loop as a table. For example, for my solution, the table would be
i | x[i]
------------------
1 | 2 * 1 + 1 = 3
2 | 2 * 2 + 1 = 5
,依此类推.这将使您了解每次迭代中for循环的作用.
and so on. This will give you an idea of what the for loop is doing at each iteration.
这篇关于解释R中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!