如何在R编程语言中进行循环?

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
For Loop
对于循环

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
Vector For Loop
循环矢量

矩阵循环(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)
LEARN MORE  Php - While and Do While Loops
了解更多PHP-While和Do While循环

嵌套循环(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
Nested For Loop
嵌套循环

使用下一条语句跳过迭代(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
Using Next Statement
使用下一条语句

翻译自: https://www.poftut.com/how-to-for-loop-in-r-programming-language/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值