R语言:循环

作者: Bioconductor | 来源:发表于2016-07-24 01:11 被阅读272次

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

  • for

  • if

  • repeat

  • which

  • while

for

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

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

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

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

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

相关文章

  • R语言for循环①

    for循环是一种重复控制结构,可以让您有效地编写一个需要执行特定次数的循环。 示例: 它产生如下结果: 下标访问 ...

  • R语言 循环

    可能有一种情况,当你需要执行一段代码几次。 通常,顺序执行语句。 首先执行函数中的第一个语句,然后执行第二个语句,...

  • R语言:循环

    这里介绍五种R语言的循环语法,分别是: for if repeat which while for if repe...

  • R语言流程控制

    都知道R语言是向量式编程,可是有时候确实需要用到循环语句,这里就介绍R语言里面的主要循环——for循环。 与其它语...

  • R语言 循环作图

  • R语言for循环练习

    for循环在R中很常见也很重要,虽然很简单,但是一定要理解其中的思想。以R语言初级作业第9题为例。加数据加载到R中...

  • R语言for循环练习

    Assignment We obtained the readcount and tpm matrix of mi...

  • R语言-0基础学习4-实战1-常见操作

    R语言学习系列R语言-0基础学习1-数据结构R语言-0基础学习2-构建子集R语言-0基础学习3-循环排序信息处理函...

  • R purrr_ for循环与函数式编程

    for 循环在R中不像其他语言中那么重要,因为R是一门函数式编程的语言。这意味着可以先将for 循环封装在函数里面...

  • R语言保存循环结果

    k <- list() for(i in 1:1000) { k[[i]] <- nn2() } newdata...

网友评论

    本文标题:R语言:循环

    本文链接:https://www.haomeiwen.com/subject/bobwjttx.html