In R language, the for loop runs slowly
for(i in 1:1000){ print(i^2) }
Supplementary: R language: for loop usage summary
Basic structure display:
vals =c(5,6,7) for(v in vals){ print(v) } # That is, run the contents in braces to each value in vals
Example display:
1. The paste() command connects several characters
For example, paste("A","B","C",sep=" ") gets "A B C", and write the following for loop on the basis of the following:
partnumber = c(1,2,5,78) for(i in partnumber){ print(paste("participant number",i, sep = " ")) } #You can get a string of participant numbers,According to the values given above, from"participant number 1" arrive"participant number 8"
2. Double 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 = " ")) } } # Loop j first, then loop i, get the following results[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" # If two loops are used, the output must be placed in the center loop. If you want the first loop, it will be placed in the outer brackets, and the second bracket will retain the last value.
3. Database instance demonstration
Titanic=("/4Gqsnz") #Read data from the network<0.2, 0.2-0.6still>0.6。
Purpose: To see the survival rate of people with different classes (Pclass) and gender (Sex) is
A<- sort(unique(Pclass)) #sort can sort categories in size order. The unique() command extracts the types of categorical variables.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"))} } }
The results are as follows:
[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"
Supplement: R language for loop generates variables in batches and assigns values
Look at the code ~
rm(list=ls()) data <- ("MS_identified_information.txt",header = T,sep = "\t",quote="", = "", = 1, = "") name1 <- paste("H1299",sep = "_",c(1:3)) name2 <- paste("Metf",sep = "_",c(1:3)) name3 <- paste("OEMetf",sep = "_",c(1:3)) name <- (name1,name2,name3) =((data)) for (i in 1:3){ tmp <- subset(data,select = (name[,i])) #Filter specific samples mean_ <- (apply(tmp, 1, mean)) #Line average //The assign() function is to assign a variable such as when i=1, df1=mean_ //Combine the three results <- (,assign(paste("df", i, sep=""), mean_)) //The variable is not reflected here, and actually the df1, df2, df3 results are generated} colnames() <- c("ID","H1299","Metf","OEMetf") (,file="MS_mean.xls", = FALSE,sep = "\t",na="")
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.