for, while, if-else,ifelse,switch
> #1 for
> for (i in 1:10) print("good")
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
[1] "good"
> x<-c(2,5,8)
> for (i in x) print(i^2)
[1] 4
[1] 25
[1] 64
> #2 while
> i<-1
> while (i<=10) {print("second");i<-i+1}
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
[1] "second"
> while(TRUE) {i<-i+1
+ if(i>10) break}
> i<-1
> repeat {i<-i+1
+ if (i>10) break}
> i
[1] 11
> #3 if-else
> data("cohesion")
> if (is.numeric(cohesion$COHO2)) cohension$COHO6 <-"study"
> if (is.numeric(cohesion$COHO2)) cohension$COHO6 <-"study" else cohension$COHO6 <-"secondtrial"
> x<-4
> y<- if (x==2) x else x+1
> y
[1] 5
> #4 ifelse
> x<-1
> ifelse(x>2,x+1,x+5)
[1] 6
> #5 switch
> feelings <-c("a","b")
> for (i in feelings)
+ print(switch (i,
+ a="sad",
+ b="happy"))
[1] "sad"
[1] "happy"
实例
> studentname <- c("John Davis", "Angela Williams", "Bullwinkle Moose", "David Jones", "Janice Markhammer", "Cheryl Cushing", "Reuven Ytzrhak", "Greg Knox", "Joel England", "Mary Rayburn")
> math <- c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522)
> science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
> English <- c(25, 22, 18, 15, 20, 28, 15, 30, 27,18)
> scoredata <- data.frame(studentname, math, science, English, stringsAsFactors=FALSE)
> z <- scale(scoredata[2:4]) # scale把数据标准化,用于比较
> z
math science English
[1,] 0.01269128 1.07806562 0.58685145
[2,] 1.14336936 1.59143020 0.03667822
[3,] -1.02568654 -0.84705156 -0.69688609
[4,] -1.64871324 -0.59036927 -1.24705932
[5,] -0.06807144 -1.48875728 -0.33010394
[6,] 0.12806660 -0.20534583 1.13702468
[7,] -1.04876160 -0.84705156 -1.24705932
[8,] 1.43180765 1.07806562 1.50380683
[9,] 0.83185601 0.30801875 0.95363360
[10,] 0.24344191 -0.07700469 -0.69688609
attr(,"scaled:center")
math science English
500.9 86.6 21.8
attr(,"scaled:scale")
math science English
86.673654 7.791734 5.452828
> Score <- apply(z, 1, mean) # apply 函数可以并行
> scoredata2 <- cbind(scoredata, Score) # 把Score 添到 scoredata 后面
> scoredata2
studentname math science English Score
1 John Davis 502 95 25 0.5592028
2 Angela Williams 600 99 22 0.9238259
3 Bullwinkle Moose 412 80 18 -0.8565414
4 David Jones 358 82 15 -1.1620473
5 Janice Markhammer 495 75 20 -0.6289776
6 Cheryl Cushing 512 85 28 0.3532485
7 Reuven Ytzrhak 410 80 15 -1.0476242
8 Greg Knox 625 95 30 1.3378934
9 Joel England 573 89 27 0.6978361
10 Mary Rayburn 522 86 18 -0.1768163
> y <- quantile(Score, c(0.8,0.6,0.4,0.2)) # 百分比
> y
80% 60% 40% 20%
0.7430341 0.4356302 -0.3576808 -0.8947579
> # ABCDE 分等级评分
> scoredata2$grade [Score >=y[1]] <-"A"
> scoredata2$grade [Score < y[1] & Score >=y[2]] <-"B"
> scoredata2$grade [Score < y[2] & Score >=y[3]] <-"C"
> scoredata2$grade [Score < y[3] & Score >=y[4]] <-"D"
> scoredata2$grade [Score < y[4]] <- "E"
> scoredata2
studentname math science English Score grade
1 John Davis 502 95 25 0.5592028 B
2 Angela Williams 600 99 22 0.9238259 A
3 Bullwinkle Moose 412 80 18 -0.8565414 D
4 David Jones 358 82 15 -1.1620473 E
5 Janice Markhammer 495 75 20 -0.6289776 D
6 Cheryl Cushing 512 85 28 0.3532485 C
7 Reuven Ytzrhak 410 80 15 -1.0476242 E
8 Greg Knox 625 95 30 1.3378934 A
9 Joel England 573 89 27 0.6978361 B
10 Mary Rayburn 522 86 18 -0.1768163 C
> name <- strsplit((scoredata2$studentname), " ")
> name
> lastname <-sapply(name, "[", 2) # "[" 是一个可以提取某个对象一部分的函数
> firstname <-sapply(name, "[", 1)
> scoredata3 <- cbind(firstname, lastname, scoredata2[,-1]) # 特异性插入
> scoredata3
firstname lastname math science English Score grade
1 John Davis 502 95 25 0.5592028 B
2 Angela Williams 600 99 22 0.9238259 A
3 Bullwinkle Moose 412 80 18 -0.8565414 D
4 David Jones 358 82 15 -1.1620473 E
5 Janice Markhammer 495 75 20 -0.6289776 D
6 Cheryl Cushing 512 85 28 0.3532485 C
7 Reuven Ytzrhak 410 80 15 -1.0476242 E
8 Greg Knox 625 95 30 1.3378934 A
9 Joel England 573 89 27 0.6978361 B
10 Mary Rayburn 522 86 18 -0.1768163 C
scoredata4 <-scoredata3 [order(lastname, firstname),] # order 排序
scoredata4
网友评论