R语言函数化学习笔记4

条件语句和循环语句

当你说话时候用到了如果,此时条件出现了
举个条件函数的例子

sign_t<-function(x){
  if(x>0){
    return(1)
  }else if(x<0){
    return(-1)
  }else{
    return(0)
  }
}            
sign_t(5)            
#[1] 1

这是简单问题,复杂的问题,不行就把需求写下来,计算机语言不过就是傻瓜式的执行你要做的事情,本来是用来减少重复操作的,语言都一样,星星你要加油喽,把之前没学过的补回来

泛化的结构

if (condition1){
   express1
} else if (condition2){
   express2
} else if (condition3){
   express3
}...
}else (condition final){
  express final
}
}

x写一个成绩的小的demo

> grade<-function(name,score){
+   if (score<70){
+     return("D")
+   }else if (score>=70&&score<80){
+     return("C")
+   }else if (score>=80&&score<90){
+     return("B")
+   }else {
+     return("A")
+   }
+ }
>  grade(89)
[1] "B"
> 

另一个小demo
namegrade<-function(name,score){
+ grade<-
+   if (score<70) "D"
+   else if (score>=70&&score<80) "C"
+     
+   else if (score>=80&&score<90)  "B"
+     
+   else "A"
+   
+   cat("the grade of",name,"is",grade)
+ }
>  namegrade("jhon",89)
the grade of jhon is B

再来一个
gradename<-function(name,score){
   if (score<70){
     grade<-"D"
     cat("what a pity!
")
   }else if (score>=70&&score<80){
     grade<-"C"
   }else if (score>=80&&score<90) {
     grade<-"B"
   }else{
     grade<-"A"
     cat("good
")
   }
   cat("the grade of",name,"is",grade)
 }
 gradename("xing",90)
##good
the grade of xing is A

因为if的本质是一个原函数,它的返回值就是满足条件分支表达式的值,所以这个表达式也可以作为内联函数

 sign_1<-function(x){
+    if (x>0) 1 else if (x<0) -1 else 0
+  }
>  sign_1(5)
[1] 1
> 

ifelse

这个函数接受一个逻辑向量作为判定条件,并且返回一个向量,对于逻辑判定条件内的每一个元素,若是true,则选择第2个参数yes中所对应的元素,若是FALSE
,则选择第3个参数no中对应的元素
小demo

  ifelse(c(TRUE,FALSE,FALSE),c(1,2,3),c(4,5,6))
[1] 1 5 6

switch 略

循环表达式

官方解释:
循环(或者迭代)通过迭代一个向量(for)或检查某个条件是否被违背(while)来重复执行某个表达式
若只是在输入值上做一些改变,而要重复执行同样的任务,目的是为了减少冗余代码

使用for循环

for循环通过迭代一个给定向量或者列表,重复执行某个表达式

语法格式

for (var in vector){
    expr
}
var 遍历vector中的各个元素的值,expr,被反复迭代执行,如果vector中有n个元素,那这个表达式等价于
var<-vector[[1]]
expr
var<-vector[[2]]
expr
var<-vector[[3]]
expr

来个小demo
```{r}
>  for (i in 1:3){
+    cat("the value of i is ",i,"
")
+  }
the value of i is  1 
the value of i is  2 
the value of i is  3 

当然啦,迭代可以作用于所有类型的向量,不仅仅是数值型的向量

for (word in c("hello","new","world")){
   cat("the word is",word,"
")
 }

或者是迭代一个列表

>  loop_list<-list(
+    a=c(1,2,3),
+    b=c("a","b","c","d"))
>  for (item in loop_list){
+    cat("item:
 lenhth:",length(item),"
 class:",class(item),"
")
+  }
item:
 lenhth: 3 
 class: numeric 
item:
 lenhth: 4 
 class: character

在迭代一个数据框的时候可以看成一个列表,但是注意数据框的列(元素)长度要一致,默认迭代的话是按照列执行的
来个小demo

>  df<-data.frame(
+    x=c(1,2,3),
+    y=c("a","b","c"),
+    stringsAsFactors = FALSE)
>  for (col in df){
+    str(col)
+  }
 num [1:3] 1 2 3
 chr [1:3] "a" "b" "c"

后面可以学一下apply函数,map,要比for好使

有时候可以利用循环外的变量来记录迭代状态或累积过程,比如求100内的和

> s<-0
> for(i in 1:100){
+   s<-s+i
+ }
> s
[1] 5050

简单的模拟一下随机游走的过程

> set.seed(123)
> x<-numeric(1000)
> for (t in 1:(length(x)-1)){
+   x[[t+1]]<-x[[t]]+rnorm(1,0,0.1)
+ }
> plot(x,type="s",main="Random walk",xlab="t")


有时候 可以中断for循环

> for (i in 1:5){
+   if(i==3) break
+   cat("message",i,"
")
+ }
message 1 
message 2 

另外可以使用next函数跳过本次迭代剩余的部分

> for (i in 1:5){
+   if(i==3) next
+   cat("message",i,"
")
+ }
message 1 
message 2 
message 4 
message 5 

循环嵌套

俩for循环套一起,常见应用,乘法口诀表

> for(i in 1:9){
+   for(j in 1:9){
+     cat("i*j=",i*j,"
")
+   }
+   
+ }
i*j= 1 
i*j= 2 
i*j= 3 
i*j= 4 
i*j= 5 
i*j= 6 
i*j= 7 
i*j= 8 
i*j= 9 
i*j= 2 
i*j= 4 
i*j= 6 
i*j= 8 
i*j= 10 
i*j= 12 
i*j= 14 
i*j= 16 
i*j= 18 
i*j= 3 
i*j= 6 
i*j= 9 
i*j= 12 
i*j= 15 
i*j= 18 
i*j= 21 
i*j= 24 
i*j= 27 
i*j= 4 
i*j= 8 
i*j= 12 
i*j= 16 
i*j= 20 
i*j= 24 
i*j= 28 
i*j= 32 
i*j= 36 
i*j= 5 
i*j= 10 
i*j= 15 
i*j= 20 
i*j= 25 
i*j= 30 
i*j= 35 
i*j= 40 
i*j= 45 
i*j= 6 
i*j= 12 
i*j= 18 
i*j= 24 
i*j= 30 
i*j= 36 
i*j= 42 
i*j= 48 
i*j= 54 
i*j= 7 
i*j= 14 
i*j= 21 
i*j= 28 
i*j= 35 
i*j= 42 
i*j= 49 
i*j= 56 
i*j= 63 
i*j= 8 
i*j= 16 
i*j= 24 
i*j= 32 
i*j= 40 
i*j= 48 
i*j= 56 
i*j= 64 
i*j= 72 
i*j= 9 
i*j= 18 
i*j= 27 
i*j= 36 
i*j= 45 
i*j= 54 
i*j= 63 
i*j= 72 
i*j= 81 
咋这么奇怪???

for循环虽然很好用,但是应该优先考虑内置函数,比如lapply等

while循环 略

notes:
在循环中:
break语句是跳出循环不再往后执行了,
next语句是跳出本次循环,进行下一次的迭代
这俩直接用就可以了,不用加()

原文地址:https://www.cnblogs.com/gaowenxingxing/p/11949849.html