前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R Programming week2 Control Structures

R Programming week2 Control Structures

作者头像
统计学家
发布2019-04-10 17:18:31
3660
发布2019-04-10 17:18:31
举报

Control Structures

Control structures in R allow you tocontrol the flow of execution of the program, depending on

runtime conditions. Common structures are:

if, else: testing a condition

for: execute a loop a fixed number of times

while: execute a loop while a condition istrue

repeat: execute an infinite loop

break: break the execution of a loop

next: skip an interation of a loop

return: exit a function

Most control structures are not used ininteractive sessions, but rather when writing functions or

longer expresisons

Control Structures: if

if(<condition>){ ## do something

} else{ ## do something else

}

if(<condition1>){ ## do something

}else if(<condition2>) { ## do something different

}else { ## do something different

}

例:

if(x> 3) {

y <- 10

}else {

y <- 0

}

Of course, the else clause is not necessary

if(<condition1>){

}

if(<condition2>){

}

for

for loops take an interator variable andassign it successive values from a sequence or vector. For loops are mostcommonly used for iterating over the elements of an object (list, vector, etc.)

for(iin 1:10) {

print(i)

}

This loop takes the i variable and in eachiteration of the loop gives it values 1, 2, 3, ..., 10, and then exits.

These following loops have the samebehavior:

x<- c("a", "b", "c", "d")

for(iin 1:4) {

print(x[i])

}

for(iin seq_along(x)) {

print(x[i])

}

for(letterin x) {

print(letter)

}

for(iin 1:4) print(x[i])

Nested for loops

for loops can be nested.

x<- matrix(1:6, 2, 3)

for(iin seq_len(nrow(x))) {

for(j in seq_len(ncol(x))) {

print(x[i, j])

}

}

Be careful with nesting though. Nestingbeyond 2–3 levels is often very difficult to read/understand

While

While loops begin by testing a condition.If it is true, then they execute the loop body. Once the loop body is executed,the condition is tested again, and so forth

count<- 0

while(count< 10) {

print(count)

count <- count + 1

}

While loops can potentially result ininfinite loops if not written properly. Use with care!

Sometimes there will be more than onecondition in the test

z<- 5

while(z>= 3 && z <= 10) {

print(z)

coin <- rbinom(1, 1, 0.5)

if(coin == 1) { ## random walk

z <- z + 1

} else {

z <- z - 1

}

}

Conditions are always evaluated from leftto right.

Repeat

Repeat initiates an infinite loop; theseare not commonly used in statistical applications but they do have their uses.The only way to exit a repeat loop is to call break.

x0<- 1

tol<- 1e-8

repeat{

x1 <- computeEstimate()

if(abs(x1 - x0) < tol) {

break

} else {

x0 <- x1

}

}

The loop in the previous slide is a bitdangerous because there’s no guarantee it will stop. Better to set a hard limiton the number of iterations (e.g. using a for loop) and then report whetherconvergence was achieved or not.

next, return

next is used to skip an iteration of a loop

for(iin 1:100) {

if(i <= 20) {

## Skip the first 20 iterations

next

}

## Do something here

}

return signals that a function should exitand return a given value

Summary

Control structures like if, while, and forallow you to control the flow of an R program

Infinite loops should generally be avoided,even if they are theoretically correct.

Control structures mentiond here areprimarily useful for writing programs; for command-line interactive work, the*apply functions are more useful.

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-04-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习与统计学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档