前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言:循环

R语言:循环

作者头像
努力在北京混出人样
发布2019-02-18 14:52:46
1.1K0
发布2019-02-18 14:52:46
举报
文章被收录于专栏:祥子的故事

这里介绍五种R语言的循环语法,分别是:

  • for
  • if
  • repeat
  • which
  • while

for

代码语言:javascript
复制
samples<- c(rep(1:10))
samples
##  [1]  1  2  3  4  5  6  7  8  9 10
for(thissample in samples){
  print(thissample)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
for ( thissample in samples){
  str <- paste(thissample,"is current sample",sep = " ")
  print (str)
}
## [1] "1 is current sample"
## [1] "2 is current sample"
## [1] "3 is current sample"
## [1] "4 is current sample"
## [1] "5 is current sample"
## [1] "6 is current sample"
## [1] "7 is current sample"
## [1] "8 is current sample"
## [1] "9 is current sample"
## [1] "10 is current sample"
for( thissample in samples){
  if (thissample == 3)
    break
  str<-paste(thissample,"is current sample" , sep = " ")
  print (str)
}
## [1] "1 is current sample"
## [1] "2 is current sample"
for(thissample in samples){
  if (thissample %% 2 == 0)
    next 
  str<-paste(thissample,"is current sample",sep = " ")
  print(str)
}
## [1] "1 is current sample"
## [1] "3 is current sample"
## [1] "5 is current sample"
## [1] "7 is current sample"
## [1] "9 is current sample"
end<-length(samples)
begin <- end -2
for(thissample in begin:end){
  str<-paste(thissample,"is current sample",sep = " ")
  print(str)
}
## [1] "8 is current sample"
## [1] "9 is current sample"
## [1] "10 is current sample"

if

代码语言:javascript
复制
samples<-c(rep(1:10))
samples
##  [1]  1  2  3  4  5  6  7  8  9 10
for(thissample in samples){
  if (thissample %% 2 != 0)
    next
  else
    print(thissample)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
ret<-ifelse(samples>6,2,1)
ret
##  [1] 1 1 1 1 1 1 2 2 2 2

repeat

代码语言:javascript
复制
total<-0
repeat{
  total<-total +1;
  print(total);
  if (total > 6)
    break;  
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
total
## [1] 7

which

代码语言:javascript
复制
which(letters == "h")
## [1] 8
data(BOD)
BOD
##   Time demand
## 1    1    8.3
## 2    2   10.3
## 3    3   19.0
## 4    4   16.0
## 5    5   15.6
## 6    7   19.8
which(BOD$demand == 16)
## [1] 4
x<-matrix(1:9,3,3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
which(x %% 3 == 0,arr.ind = TRUE) #返回位置
##      row col
## [1,]   3   1
## [2,]   3   2
## [3,]   3   3
which(x %% 3 == 0,arr.ind = FALSE) # 返回数
## [1] 3 6 9

while

代码语言:javascript
复制
x<-1
while(x<5){
  x<-x+1
  print(x)
}
## [1] 2
## [1] 3
## [1] 4
## [1] 5
x<-1
while(x<5){
  x<-x+1
  if(x == 3)
    break
  print(x)
}
## [1] 2
x<-1
while(x<5){
  x<-x+1
  if(x == 3)
    next
  print (x)
}
## [1] 2
## [1] 4
## [1] 5
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年07月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

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