函数自己调用自己
zhang1
> zhang1 <- function(x)
+ {
+ if(x==0) # 设置终止条件,防止死循环
+ x_sum=1
+ else
+ x_sum=x*zhang_1(x-1) #自己调用自己
+ return(x_sum)
+ }
> zhang1(5)
[1] 120
各种循环
zhang2
> ## 1度电50元,如果一个月使用超过200度,电费再加收15%。如果电费小于1元,以四舍五入处理。
>
> zhang2 <- function(deg, unitprice = 50)
+ {
+ net.price <- deg * unitprice
+ if (deg > 200) #如果超过200度,那么……
+ {
+ net.price <- net.price * 1.15
+ }
+ round (net.price)
+ }
> zhang2(deg = 150)
[1] 7500
> zhang2(deg = 250)
[1] 14375
zhang3
## 1度电50元,如果一个月使用超过100度,电费再加收15%,如果100度(含)以下,电费八五折。如果电费小于1元,以四舍五入处理。
zhang3 <- function( deg, unitprice = 50)
{
net.price <- deg * unitprice
if (deg > 100)
net.price <- net.price * 1.15 #注意形式有何不同
else
net.price <- net.price *0.85
round(net.price)
}
zhang3(80)
zhang3(100)
zhang4
# if语句可有返回值,改写上例
zhang4 <- function(deg, unitprice = 50)
{
net.price <- deg * unitprice
adjustment <- if (deg > 100) 1.15 else 0.85 # if语句返回值
total.price <- net.price * adjustment
round(total.price)
}
zhang4(deg = 80)
zhang4(deg = 200)
zhang5
# 接上例子,更改
zhang5 <- function( deg, unitprice = 50)
{
net.price <- deg * unitprice
total.price <- net.price * if (deg >100) 1.15 else 0.85 # if语句直接应用在表达式中
round(total.price)
}
zhang5(deg = 80)
zhang5(deg = 200)
zhang6
## 1度电50元,超过120度,加收15%,小于80度,减免15%。
zhang6 <- function( deg, unitprice = 50)
{
if (deg >120)
net.price <- deg * unitprice * 1.15
else if (deg < 80) # 多重判断时可用的上
net.price <- deg * unitprice * 0.85
else
net.price <- deg * unitprice
round(net.price)
}
zhang6(70) # 注意形式有何不同
zhang6(100)
zhang6(150)
zhang7
## 1度电50元,超过100度,加收15%,小于100度(含),减免15%。有贫困证又用电小于100度,再减免3成。电费又小于1元的部分,四舍五入。
zhang7 <- function( deg, poor = FALSE, unitprice = 50)
{
net.price <- deg * unitprice
if( deg > 100)
net.price <- net.price * 1.15
else
{
net.price <- net.price * 0.85
if (poor == TRUE) # 注意,嵌套的if
net.price = net.price *0.7
}
round(net.price)
}
zhang7(deg = 80)
zhang7(deg=80, poor = TRUE) # 注意,不要写成 poor == TURE
zhang7(deg=200)
zhang7(deg=200, poor = TRUE)
zhang8
## if不能处理向量,ifelse可以
## if(逻辑判断, T表达式,F表达式)
ifelse(c(1,5) >3, 10, 1)
## 一度电50块,用电100度(含)以下,八五折,100度以上,电费加收15%。
zhang8 <- function(deg, unitprice = 50)
{
net.price <- deg * unitprice
net.price = net.price * ifelse((deg > 100), 1.15, 0.85) #注意用到的是ifelse
round(net.price)
}
zhang8(c(80,200))
zhang9
## switch.之前的if……else用于多重判断,这也可用switch语句。注意,它不能用于向量。
## switch(判断,表达式1,表达式2,……)判断之后的值可能是数字或文字,如果是1,执行表达式1,是2,执行表达式2……如果是文字,执行对应得的表达式。
## 1度电50元,超过120度,加收15%,小于80度,减免15%。
zhang9 <- function(deg, unitprice = 50)
{
if(deg > 120) index <- 1
if(deg <= 120 & deg >= 80) index <- 2
if(deg < 80) index <- 3 # 创建3个索引
switch(index,
net.price <- deg * unitprice * 1.15,
net.price <- deg * unitprice,
net.price <- deg * unitprice * 0.85)
round(net.price)
}
zhang9(deg = 70)
zhang9(deg = 100)
zhang9(deg = 150)
zhang10
## for循环。可用于向量对象的操作,格式 for(循环索引 in 区间) 单一运算指令。多个指令,则如下
## for(循环索引 in 区间) {系列运算命令}
##计算1到n的和。
zhang10 <- function(n)
{
sum_n <- 0
for(i in n) sum_n <- sum_n + i
print(sum_n)
}
zhang10(1:2)
zhang11
> ##使用内建数据集合state.region,找出属于“North Centra”的州有几个。
> zhang11 <- function(n)
+ {
+ counter <- 0
+ for(i in n)
+ {
+ if(i == "North Central")
+ counter <- counter + 1
+ }
+ print(counter)
+ }
> state.region # 看一眼长啥样
[1] South West West South West
[6] West Northeast South South South
[11] West West North Central North Central North Central
[16] North Central South South Northeast South
[21] Northeast North Central North Central South North Central
[26] West North Central West Northeast Northeast
[31] West Northeast South North Central North Central
[36] South West Northeast Northeast South
[41] North Central South South West Northeast
[46] South West South North Central West
Levels: Northeast South North Central West
> zhang11(state.region)
[1] 12
zhang12
> ##看一眼state.x77
> state.x77
Population Income Illiteracy Life Exp Murder HS Grad Frost Area
Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
California 21198 5114 1.1 71.71 10.3 62.6 20 156361
Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
Connecticut 3100 5348 1.1 72.48 3.1 56.0 139 4862
Delaware 579 4809 0.9 70.06 6.2 54.6 103 1982
Florida 8277 4815 1.3 70.66 10.7 52.6 11 54090
Georgia 4931 4091 2.0 68.54 13.9 40.6 60 58073
Hawaii 868 4963 1.9 73.60 6.2 61.9 0 6425
Idaho 813 4119 0.6 71.87 5.3 59.5 126 82677
Illinois 11197 5107 0.9 70.14 10.3 52.6 127 55748
Indiana 5313 4458 0.7 70.88 7.1 52.9 122 36097
Iowa 2861 4628 0.5 72.56 2.3 59.0 140 55941
Kansas 2280 4669 0.6 72.58 4.5 59.9 114 81787
Kentucky 3387 3712 1.6 70.10 10.6 38.5 95 39650
Louisiana 3806 3545 2.8 68.76 13.2 42.2 12 44930
Maine 1058 3694 0.7 70.39 2.7 54.7 161 30920
Maryland 4122 5299 0.9 70.22 8.5 52.3 101 9891
Massachusetts 5814 4755 1.1 71.83 3.3 58.5 103 7826
Michigan 9111 4751 0.9 70.63 11.1 52.8 125 56817
Minnesota 3921 4675 0.6 72.96 2.3 57.6 160 79289
Mississippi 2341 3098 2.4 68.09 12.5 41.0 50 47296
Missouri 4767 4254 0.8 70.69 9.3 48.8 108 68995
Montana 746 4347 0.6 70.56 5.0 59.2 155 145587
Nebraska 1544 4508 0.6 72.60 2.9 59.3 139 76483
Nevada 590 5149 0.5 69.03 11.5 65.2 188 109889
New Hampshire 812 4281 0.7 71.23 3.3 57.6 174 9027
New Jersey 7333 5237 1.1 70.93 5.2 52.5 115 7521
New Mexico 1144 3601 2.2 70.32 9.7 55.2 120 121412
New York 18076 4903 1.4 70.55 10.9 52.7 82 47831
North Carolina 5441 3875 1.8 69.21 11.1 38.5 80 48798
North Dakota 637 5087 0.8 72.78 1.4 50.3 186 69273
Ohio 10735 4561 0.8 70.82 7.4 53.2 124 40975
Oklahoma 2715 3983 1.1 71.42 6.4 51.6 82 68782
Oregon 2284 4660 0.6 72.13 4.2 60.0 44 96184
Pennsylvania 11860 4449 1.0 70.43 6.1 50.2 126 44966
Rhode Island 931 4558 1.3 71.90 2.4 46.4 127 1049
South Carolina 2816 3635 2.3 67.96 11.6 37.8 65 30225
South Dakota 681 4167 0.5 72.08 1.7 53.3 172 75955
Tennessee 4173 3821 1.7 70.11 11.0 41.8 70 41328
Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134
Utah 1203 4022 0.6 72.90 4.5 67.3 137 82096
Vermont 472 3907 0.6 71.64 5.5 57.1 168 9267
Virginia 4981 4701 1.4 70.08 9.5 47.8 85 39780
Washington 3559 4864 0.6 71.72 4.3 63.5 32 66570
West Virginia 1799 3617 1.4 69.48 6.7 41.6 100 24070
Wisconsin 4589 4468 0.7 72.48 3.0 54.5 149 54464
Wyoming 376 4566 0.6 70.29 6.9 62.9 173 97203
##计算state.x77中的美国人口总数。
> zhang12 <- function(n)
+ {
+ zhang.sum <- 0
+ for( i in state.x77[,"Population"])
+ zhang.sum <- zhang.sum + i
+ print(zhang.sum)
+ }
> zhang12(state.x77[,"Population"])
[1] 212321
zhang13
> ## 一度电50块。用电超过150度,打8折。此外政府机关8折,公司用加收2成,普通家庭收费标准不变。
> deginfo <- c(80, 80, 200, 200)
> custinfo <- c("goverment", "company", "company", "family")
> zhang13 <- function(deg, customer, unitprice = 50)
+ {
+ listprice <- deg * unitprice * ifelse(deg > 150, 0.8, 1)
+ adj <- numeric(0) # 这个地方有意思。值为numeric(0)的对象长度为0,
+ # 假设一个对象x,用length(x) == 0 来判断对象x是不是numeric(0)
+ #还是谷歌清楚:numeric(0) returns a numeric vector of *length 0*,
+ #so when you add anything to it you get the same result (it's basically a numeric NULL)
+ for(i in customer)
+ adj <- c(adj, switch(i, goverment = 0.8, company = 1.2, family = 1))
+ finalprice <- listprice * adj
+ round(finalprice)
+ }
> zhang13(deginfo, custinfo)
[1] 3200 4800 9600 8000
zhang14
> #这事,也可以这么干
> deginfo <- c(80, 80, 200, 200)
> custinfo <- c("goverment", "company", "company", "family")
> zhang14 <- function(deg, customer, unitprice = 50)
+ {
+ listprice <- deg * unitprice * ifelse(deg > 150, 0.8, 1)
+ num.customer <- length(customer)
+ adj <- numeric(num.customer)
+ for(i in seq_along(customer)) # 赋值字符向量给一个对象,然后对其seq_along试试
+ adj[i] <- switch(customer[i], goverment = 0.8, company = 1.2, 1)
+ finalprice <- listprice * adj
+ round(finalprice)
+ }
> zhang14(deginfo, custinfo)
[1] 3200 4800 9600 8000
zhang15
> ## while循环
> ## while(逻辑表达式){系列运算指令} 如果逻辑表达式为T,循环继续,直到为F
> ## 1到n的和
> zhang15 <- function(x)
+ {
+ sum.x <- 0
+ while(x >= 0)
+ {
+ sum.x <- sum.x + x
+ x <- x-1
+ }
+ return(sum.x)
+ }
> zhang15(10)
[1] 55
zhang16
> ##1到n的和
> zhang16 <- function(x)
+ {
+ Sum.x <- 0
+ repeat
+ {
+ Sum.x <- Sum.x + x
+ if (x==0) break # 跳出循环
+ x <- x-1
+ }
+ return(Sum.x)
+ }
> zhang16(10)
[1] 55
zhang17
## break循环也可以和for循环和while循环配合,可立即跳出循环。
## 1到n的和
> zhang17 <- function(n)
+ {
+ sum.x <- 0
+ for(i in n)
+ {
+ if(sum.x + i > 3000) break
+ sum.x <- sum.x + i
+ }
+ return(sum.x) # print()也可
+ }
> zhang17(1:50)
[1] 1275
> zhang17(1:100) # 计算到72时,和是2926,到73时超范围,break跳出,打印结果。
[1] 2926
zhang18
## next语句。和break一样,须与if,即逻辑表达配合使用,但next只是跳过本次循环,不是跳出循环
##1到n的偶数和
> zhang18 <- function(n)
+ {
+ Sum.x <- 0
+ for(i in n)
+ {
+ if(i %% 2 != 0) next # 关键步骤
+ Sum.x <- Sum.x + i
+ }
+ print(Sum.x)
+ }
> zhang18(1:10)
[1] 30
> zhang18(1:100)
[1] 2550