函数已有主体(关于循环与自编函数(R))
函数已有主体(关于循环与自编函数(R)),哪吒游戏网给大家带来详细的函数已有主体(关于循环与自编函数(R))介绍,大家可以阅读一下,希望这篇函数已有主体(关于循环与自编函数(R))可以给你带来参考价值。
For循环
结构:for(i in seq){
body}
其中seq是一条数值或字符串的求值语句。通过具体例子来看R在循环中的逻辑:
> for (i in 2010:2020){
+ print(paste("this year is",i))
+ }
[1] "this year is 2010"
[1] "this year is 2011"
[1] "this year is 2020"
[1] "this year is 2013"
[1] "this year is 2020"
[1] "this year is 2020"
> a> for(i in a){
+ print("truth is here")
+ }
[1] "truth is here"
[1] "truth is here"
[1] "truth is here"
while循环
结构:while(cond) statement
While语句在使用的过程中要确保括号内cond的条件语句可以改变,否则会循环不止!通过例子来学习,在例子中找到共性:
> x> while(x+ x+ print(x)
+ }
[1] 2
[1] 3
[1] 4
[1] 5
当x=3时:
> x> while(x+ x+ if(x==3)
+ print(x)
+ }
[1] 3
> v> cnt> while(cnt+ cnt=cnt+1
+ print(v)
+ }
[1] "hello" "while" "loop"
[1] "hello" "while" "loop"
[1] "hello" "while" "loop"
[1] "hello" "while" "loop"
[1] "hello" "while" "loop"
> y> while(y+ y+ print(y)
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
while()循环将执行一段命令直到条件不再满足。
条件语句:
if...else...结构
模板:
if(cond){
body
}else{
body
}
首先需要注意的是if内的(cond)必须是一个最终被解析为T或F的表达式!所以要尽可能让cond简单。
t> if(2 %in% t){
+ print("2 in t")
+ }else{
+ print("didn't found 2")
+ }
[1] "2 in t"
x if("Truth" %in% x) {
print("Truth is found ")
} else {
print("Truth is not found") }
[1] Truth is found
#看到了吗,%in%非常好使函数已有主体,要看一个元素是否在某个向量里用它就好啦#
if...else...也可以在一行内搞定:
xy3)7 else 8
y
[1] 7
#在这个例子中,返回值只能是数字,换成字母就玩不转了#
if..else...结构用于仅有两个选择,当有三个选择的时候就需要用到嵌套了。当然四个及以上选择理论上也可以,但是那很有可能会是一个逻辑上的大麻烦。
x count for (val in x) {
if(val %% 2 == 0) count = count+1
print(count)
}
[1] 3
for (i in 1:10) {
if (!i %% 2){
next
}
print(i)
}
1
3
5
7
9
switch()
结构:
switch(statement, list)
switch()函数会返回括号中数字相对应的值,如果数值超出范围(大于列表中的项数或小于1),则会返回NULL。
> switch(2,"red","green","blue")
[1] "green"
> switch(1,"red","green","blue")
[1] "red"
> x > x
NULL
> x > x
NULL
语句的结果也可以是字符串。在这种情况下,返回匹配的命名项的值。
> switch("color", "color" = "red", "shape" = "square", "length" = 5)
[1] "red"
> switch("length", "color" = "red", "shape" = "square", "length" = 5)
[1] 5
自编函数
结构:
myfunctionbody
return()
}
自编函数在R中主要有两个目的,辅助检出代码中的错误,还有将重复计算程式化,提高效率。在这个结构中,myfunction是对自编函数的命名,最好能够体现出函数的功能。(arg1,arg2...)是在使用自编函数时参数,body是自编函数的主体,return()是对自编函数的终结,主体部分的运算到return就会终止,这里需要注意的是,如果返回值不止一个时函数已有主体,在return中要以向量的形式返回。
> add<-function(x,y){
+ z<-x+y
+ return(z)
+ }
> add(7,8)
[1] 15
>
下面看一个转置的自编函数:
mytrans<-function(x){
+ if(!is.matrix(x)){
+ warning("argument is not matrix:returning:NA")
+ return(NA_real_)
+ }
+ y<-matrix(1,nrow=ncol(x),ncol=nrow(x))
+ for(i in 1:nrow(x)){
+ for(j in 1:ncol(x)){
+ y[j,i]<-x[i,j]
+ }
+ }
+ return(y)
+ }
> z<-matrix(1:10,nrow=5,ncol=2)
> mytrans(z)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
> z
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
> mydate+ switch(type,
+ long=format(Sys.time(),"%A %B %d %Y"),
+ short=format(Sys.time(),"%m-%d-%y"),
+ cat(type,"is not a recognized type\n")
+ )
+ }
> mydate("long")
[1] "星期三 七月 05 2020"
> mydata("short")
[1] "07-05-17"
> mydata()
[1] "星期三 七月 05 2020"
> mydata("good")
good is not a recognized type
作为R的初学者,在自己敲代码的同时也希望通过更多例子了解更多,这些例子比较简单,复杂的看不懂,没有贴出来,希望在以后的学习中了解更多。
总结:以上内容就是针对函数已有主体(关于循环与自编函数(R))详细阐释,如果您觉得有更好的建议可以提供给哪吒游戏网小编,函数已有主体(关于循环与自编函数(R))部分内容转载自互联网,有帮助可以收藏一下。
