R语言中,for循环运行比较慢
1
2
3
|
for (i in 1:1000){
print(i^2)
}
|
补充:R语言:for循环使用小结
基本结构展示:
1
2
3
4
5
|
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:
1
2
3
4
5
|
partnumber = c(1,2,5,78)
for (i in partnumber){
print( paste ( "participant number" ,i, sep = " " ))
}
#就可以得到一串参与者号码,根据上面给定的几个值, 从"participant number 1" 到"participant number 8"
|
2. 双重loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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. 数据库实例演示
1
|
Titanic= read .csv( "https://goo.gl/4Gqsnz" ) #从网络读取数据<0.2, 0.2-0.6还是>0.6。
|
目的:看不同舱位(Pclass)和不同性别(Sex)的人的生存率是
1
2
3
4
5
6
7
8
9
10
11
12
|
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"
补充:R语言for循环批量生成变量,并且赋值
看代码~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
rm (list= ls ())
data <- read .table( "MS_identified_information.txt" ,header = T,sep = "\t" ,quote= "" ,na.strings = "" ,row.names = 1,comment.char = "" )
name1 <- paste ( "H1299" ,sep = "_" ,c(1:3))
name2 <- paste ( "Metf" ,sep = "_" ,c(1:3))
name3 <- paste ( "OEMetf" ,sep = "_" ,c(1:3))
name <- data.frame(name1,name2,name3)
mean.data=data.frame(row.names(data))
for (i in 1:3){
tmp <- subset(data, select = as.vector.factor(name[,i])) #筛选特定的样本
mean_ <- as.data.frame(apply(tmp, 1, mean)) #行求平均值
//assign ()功能就是对变量进行赋值如i=1时,df1=mean_
// 把三次结果组合起来
mean.data <- cbind.data.frame(mean.data,assign( paste ( "df" , i, sep= "" ), mean_))
// 这里没有体现出变量,实际上生成了df1,df2,df3结果
}
colnames(mean.data) <- c( "ID" , "H1299" , "Metf" , "OEMetf" )
write.table(mean.data, file = "MS_mean.xls" ,row.names = FALSE,sep = "\t" ,na= "" )
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/data_007/article/details/74013922