## 9.1 R循环介绍

### 9.1.1 简单示例

total <- 0
for(i in 1:100){
total <- total+i
}
print(paste0('1到100连续相加求和等于:',total))
#> [1] "1到100连续相加求和等于:5050"

# loop structure
# for (var in seq) {expr}

### 9.1.2 循环结构

R中有三种循环结构：

• for

for循环是大家使用最多的循环结构，for循环的迭代次数是固定的，并且事先知道。如最开始的示例中，1连续相加到100,共计加法次数是100次。

for循环示例如下：

library(tidyverse)
df <- tibble(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)

output <- vector("double", ncol(df))  # 1. output
for (i in seq_along(df)) {            # 2. sequence
output[[i]] <- median(df[[i]])      # 3. body
}
output
#> [1] -0.2458 -0.2873 -0.0567  0.1443

seq_along可以?seq查看用法.

You might not have seen seq_along() before. It’s a safe version of the familiar 1:length(l), with an important difference: if you have a zero-length vector, seq_along() does the right thing:

#wrong
seq_along(c())
#> integer(0)
1:length(c())
#> [1] 1 0

# generates the integer sequence 1, 2, ..., length(along.with). (along.with is usually abbreviated to along, and  seq_along is much faster.)

• while

readinteger <- function(){
}

while (response!=42) {
print("Sorry, the answer to whatever the question MUST be 42");
}
• Repeat

repeat循环与while循环类似。如下所示，直到满足if条件后才会跳出循环结构。

i <- 1
total <- 0
repeat{
total <- total+i
i <- i+1
if(i > 100){
print(paste0('连续相加求和等于:',total))
break
}
}
#> [1] "连续相加求和等于:5050"

### 9.1.3 循环控制

R中如何中断或退出循环呢？除了自然结束的for循环，while,repeat是如何结束的呢，在R中，我们可以通过break以及next控制循环，上一示例中我们已经看到break是如何跳出循环的。

• next 用法
for(i in letters[1:6] ){
if(i == "d"){
next
}
print(i)
}
#> [1] "a"
#> [1] "b"
#> [1] "c"
#> [1] "e"
#> [1] "f"
• break 用法

m=10
n=10
ctr=0
mymat = matrix(0,m,n)

for(i in 1:m) {
for(j in 1:n) {
if(i==j) {
break;
} else {
# you assign the values only when i<>j
mymat[i,j] = i*j
ctr=ctr+1
}
}
print(i*j)
}
#> [1] 1
#> [1] 4
#> [1] 9
#> [1] 16
#> [1] 25
#> [1] 36
#> [1] 49
#> [1] 64
#> [1] 81
#> [1] 100
# 共赋值多少次
print(ctr)
#> [1] 45

### 9.1.4 嵌套循环

# not run
v <- vector(length = 100)
for(i in 1:10){
for(j in 1:10){
v[i*j] = i * j
}
}