R is a programming language which is mainly used for statistical work. We can use for
loops in R in different ways. We can use R for
loop with the vector data type and in a regular way like other programming languages too.
R是一种编程语言,主要用于统计工作。 我们可以以不同的方式在R中使用for
循环。 我们可以使用R for
矢量数据类型的循环,并且可以像其他编程语言一样以常规方式使用R。
句法 (Syntax)
The syntax of the for loop is like below.
for循环的语法如下。
for (VAL in SEQUENCE)
{
STATEMENT
}
- VAL is used in each iteration to set current element value. 在每次迭代中都使用VAL来设置当前元素值。
- SEQUENCE is the list, an array of the values those will be iterated. SEQUENCE是列表,这些值将被迭代的数组。
- STATEMENT will be executed in each iteration. We can also call is the body of the for loop. STATEMENT将在每次迭代中执行。 我们也可以调用for循环的主体。
对于循环 (For Loop)
We will start with a simple for loop example. We will iterate over the given list which will start from 1
and count to the 10
.We will print each element in each iteration.
我们将从一个简单的for循环示例开始。 我们将遍历给定的列表,该列表将从1
开始计数到10
我们将在每次迭代中打印每个元素。
for(i in 1:10)
{
print(i)
}
![For Loop](https://img-blog.csdnimg.cn/img_convert/999cc469118a6606093dae1f19750eea.png)
We can see that each element is printed to the standard output.
我们可以看到每个元素都被打印到标准输出中。
循环矢量 (Vector For Loop)
Vector is a very popular and useful data structure in R programming language. We will create a Vector which holds years from 2015 to 2019. Then we will iterate over this vector and print each element to the standard output.
在R编程语言中,Vector是非常流行且有用的数据结构。 我们将创建一个向量,该向量保存从2015年到2019年的年。然后,我们将对该向量进行迭代,并将每个元素打印到标准输出中。
for (year in c(2015,2016,2017,2018,2019)){
print(paste("Year:", year))
}
![Vector For Loop](https://img-blog.csdnimg.cn/img_convert/6f2b6e0249f0f43a962fad6371fc88fa.png)
矩阵循环(Matrix For Loop)
Matrix is another useful data type used in the R programming language. We can also call matrix as multi-level array or vector. We can iterate over given matrix by using multiple and nested for loops. We will create a matrix which is 4×4 and then print to the screen accordingly.
矩阵是R编程语言中使用的另一种有用的数据类型。 我们也可以将矩阵称为多级数组或向量。 我们可以通过使用多个和嵌套的for循环来迭代给定的矩阵。 我们将创建一个4×4的矩阵,然后相应地打印到屏幕上。
mymatrix <- matrix(1:16,nrow=4,ncol=4)
for(i in 1:dim(mymatrix[1])){
for(j in 1:dim(mymatrix)[2]){
mymatrix[i,j]=i*j
}
}
print(mymatrix)
![](https://img-blog.csdnimg.cn/img_convert/0c809744b1c9a554e88e9724de988cd7.png)
嵌套循环(Nested For Loop)
We have all ready used some nested for loop in previous example. We can use nested or multi level loops with for like below. We will use 3 level and start from 1 to 3 and multiple them.
我们已经准备好在前面的示例中使用一些嵌套的for循环。 我们可以使用for嵌套或多级循环,如下所示。 我们将使用3级,并从1到3并将它们乘以3。
for(i in 1:3){
for(j in 1:3){
for(k in 1:3){
print(i*j*k)
}
}
}
![Nested For Loop](https://img-blog.csdnimg.cn/img_convert/b086114b9dde597d4381e7bc4b2f3313.png)
使用下一条语句跳过迭代(Using Next Statement To Skip Iteration)
During the iteration of the for loop we may need to skip for specific iterations. We can use next
statement to skip specified iterations or cases. In this example, we will skip odd numbers.
在for循环的迭代过程中,我们可能需要跳过特定的迭代。 我们可以使用next
语句跳过指定的迭代或案例。 在此示例中,我们将跳过奇数。
for(i in 1:10){
if (!i%%2){
next
}
print(i)
}
![Using Next Statement](https://img-blog.csdnimg.cn/img_convert/da776f5876c6c1b75376114e171c7e3b.png)
翻译自: https://www.poftut.com/how-to-for-loop-in-r-programming-language/