您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用R語言替換for循環(huán),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
for(i in 1:1000){ print(i^2) }
補(bǔ)充:R語言:for循環(huán)使用小結(jié)
vals =c(5,6,7) for(v in vals){ print(v) } #即把大括號(hào)里的內(nèi)容對(duì)vals里的每一個(gè)值都循環(huán)run一遍
如paste("A","B","C",sep=" ")得到的就是“A B C”,在次基礎(chǔ)上寫如下for loop:
partnumber = c(1,2,5,78) for(i in partnumber){ print(paste("participant number",i, sep = " ")) } #就可以得到一串參與者號(hào)碼,根據(jù)上面給定的幾個(gè)值, 從"participant number 1" 到"participant number 8"
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 = " ")) } } # 先對(duì)j循環(huán),后對(duì)i循環(huán),得到如下結(jié)果 [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" # 兩個(gè)loop的話,output得放最中心的loop里面,如果只要要第一層loop,就放在靠外一層括號(hào)里面,第二層括號(hào)就保留最后的一個(gè)值
Titanic=read.csv("https://goo.gl/4Gqsnz") #從網(wǎng)絡(luò)讀取數(shù)據(jù)<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"))} } }
結(jié)果如下:
[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"
補(bǔ)充:R語言for循環(huán)批量生成變量,并且賦值
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()功能就是對(duì)變量進(jìn)行賦值如i=1時(shí),df1=mean_ //把三次結(jié)果組合起來 mean.data <- cbind.data.frame(mean.data,assign(paste("df", i, sep=""), mean_)) //這里沒有體現(xiàn)出變量,實(shí)際上生成了df1,df2,df3結(jié)果 } colnames(mean.data) <- c("ID","H1299","Metf","OEMetf") write.table(mean.data,file="MS_mean.xls",row.names = FALSE,sep = "\t",na="")
關(guān)于如何使用R語言替換for循環(huán)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。