R语言:for循环使用小结
2017-06-26 15:16
260 查看
基本结构展示:
vals =c(5,6,7)
for(v in vals){
print(v)
}
#即把大括号里的内容对vals里的每一个值都循环run一遍
实例展示:
1. paste() 命令是把几个字符连接起来,如paste("A","B","C",sep=" ")得到的就是“A B C”,在次基础上写如下for loop:
partnumber = c(1,2,5,78)
for(i in partnumber){
print(paste("participant number",i, sep = " "))
}
#就可以得到一串参与者号码,根据上面给定的几个值, 从"participant number 1" 到"participant number 8"
2. 双重loop
partnumber = c(1,2,5,78)
institution =c("cancer center", "RMH", "Florey")
for(i in partnumber){
for(j in institution){
print(paste("participant number",i,", institution",j,sep = " "))
}
}
# 先对j循环,后对i循环,得到如下结果
[1] "participant number 1 , institution cancer center"
[1] "participant number 1 , institution RMH"
[1] "participant number 1 , institution Florey"
[1] "participant number 2 , institution cancer center"
[1] "participant number 2 , institution RMH"
[1] "participant number 2 , institution Florey"
[1] "participant number 5 , institution cancer center"
[1] "participant number 5 , institution RMH"
[1] "participant number 5 , institution Florey"
[1] "participant number 78 , institution cancer center"
[1] "participant number 78 , institution RMH"
[1] "participant number 78 , institution Florey"
# 两个loop的话,output得放最中心的loop里面,如果只要要第一层loop,就放在靠外一层括号里面,第二层括号就保留最后的一个值
3. 数据库实例演示
Titanic=read.csv("https://goo.gl/4Gqsnz") #从网络读取数据<0.2, 0.2-0.6还是>0.6。
目的:看不同舱位(Pclass)和不同性别(Sex)的人的生存率是
A<- sort(unique(Pclass)) #sort可以把类别按大小顺序排,unique()命令是把分类变量的种类提取出来
B<- sort(unique(Sex))
for(i in A){
for(j in B){
if(mean(Survived[Pclass==i&Sex==j])<0.2){
print(paste("for class",i,"sex",j,"mean survival is less than 0.2"))
} else if (mean(Survived[Pclass==i&Sex==j])>0.6){
print(paste("for class",i,"sex",j,"mean survival is more than 0.6"))
} else {
print(paste("for class",i,"sex",j,"mean survival is between 0.2 and 0.6"))}
}
}
结果如下:
[1] "for class 1 sex female mean survival is more than 0.6"
[1] "for class 1 sex male mean survival is between 0.2 and 0.6"
[1] "for class 2 sex female mean survival is more than 0.6"
[1] "for class 2 sex male mean survival is less than 0.2"
[1] "for class 3 sex female mean survival is between 0.2 and 0.6"
[1] "for class 3 sex male mean survival is less than 0.2"
vals =c(5,6,7)
for(v in vals){
print(v)
}
#即把大括号里的内容对vals里的每一个值都循环run一遍
实例展示:
1. paste() 命令是把几个字符连接起来,如paste("A","B","C",sep=" ")得到的就是“A B C”,在次基础上写如下for loop:
partnumber = c(1,2,5,78)
for(i in partnumber){
print(paste("participant number",i, sep = " "))
}
#就可以得到一串参与者号码,根据上面给定的几个值, 从"participant number 1" 到"participant number 8"
2. 双重loop
partnumber = c(1,2,5,78)
institution =c("cancer center", "RMH", "Florey")
for(i in partnumber){
for(j in institution){
print(paste("participant number",i,", institution",j,sep = " "))
}
}
# 先对j循环,后对i循环,得到如下结果
[1] "participant number 1 , institution cancer center"
[1] "participant number 1 , institution RMH"
[1] "participant number 1 , institution Florey"
[1] "participant number 2 , institution cancer center"
[1] "participant number 2 , institution RMH"
[1] "participant number 2 , institution Florey"
[1] "participant number 5 , institution cancer center"
[1] "participant number 5 , institution RMH"
[1] "participant number 5 , institution Florey"
[1] "participant number 78 , institution cancer center"
[1] "participant number 78 , institution RMH"
[1] "participant number 78 , institution Florey"
# 两个loop的话,output得放最中心的loop里面,如果只要要第一层loop,就放在靠外一层括号里面,第二层括号就保留最后的一个值
3. 数据库实例演示
Titanic=read.csv("https://goo.gl/4Gqsnz") #从网络读取数据<0.2, 0.2-0.6还是>0.6。
目的:看不同舱位(Pclass)和不同性别(Sex)的人的生存率是
A<- sort(unique(Pclass)) #sort可以把类别按大小顺序排,unique()命令是把分类变量的种类提取出来
B<- sort(unique(Sex))
for(i in A){
for(j in B){
if(mean(Survived[Pclass==i&Sex==j])<0.2){
print(paste("for class",i,"sex",j,"mean survival is less than 0.2"))
} else if (mean(Survived[Pclass==i&Sex==j])>0.6){
print(paste("for class",i,"sex",j,"mean survival is more than 0.6"))
} else {
print(paste("for class",i,"sex",j,"mean survival is between 0.2 and 0.6"))}
}
}
结果如下:
[1] "for class 1 sex female mean survival is more than 0.6"
[1] "for class 1 sex male mean survival is between 0.2 and 0.6"
[1] "for class 2 sex female mean survival is more than 0.6"
[1] "for class 2 sex male mean survival is less than 0.2"
[1] "for class 3 sex female mean survival is between 0.2 and 0.6"
[1] "for class 3 sex male mean survival is less than 0.2"
相关文章推荐
- block使用小结、在arc中使用block、如何防止循环引用
- Python的for和break循环结构中使用else语句的技巧
- 取出json中[{},{}] 的数据 ,使用 for-in 来循环对象的属性
- for循环和while循环在使用continue时有所区别
- block使用小结、在arc中使用block、如何防止循环引用
- PB12.5中的循环语句使用(FOR...NEXT和DO ...LOOP)
- java学习11--语句:for循环嵌套、转义字符的使用
- 循环语句小结 for while do-while
- UVA10010使用数组和一个for循环控制前进路径方向不变
- 黑马程序员_增强for循环小结
- 使用 let 解决for 循环闭包 i变量问题
- 黑马程序员 十四、Eclipse 的使用、增强 for 循环、自动拆箱与装箱等
- 命令行参数的使用、goto标签的使用以及for循环中局部变量声明问题
- 深入应用c++11读书笔记--使用c++11让程序更简洁、更现代-2.自己实现一个支持范围for循环的类~
- sql 游标的使用―游标FOR循环小例子
- IAR For AVR 定时器溢出中断 (使用小结)
- javascript中for/in循环及使用技巧
- 15、C#里面的for循环结构的使用
- 3.7.12 使用for循环打印下面的图形
- PHP学习第一节----for循环的应用及对date函数的初步使用