for(i in 1:1000){print(i^2)}
补充:r语言:for循环使用小结
vals =c(5,6,7)for(v in vals){ print(v)}#即把大括号里的内容对vals里的每一个值都循环run一遍
如paste(“a”,”b”,”c”,p=” “)得到的就是“a b c”,在次基础上写如下for loop:
partnumber = c(1,2,5,78)for(i in partnumber){ print(paste("participant number",i, p = " ")) }#就可以得到一串参与者号码,根据上面给定的几个值, 从"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,p = " "))}}# 先对j循环,后对i循环,得到如下结果[1] "participant number 1 , institution cancer center"[1] "participant number 1 , institution rmh"[1] "participant number 1 , institution florey"[1] "part电子商务发展现状icipant number 2 , institution cancer center"[1] "participant nu张槐乾mber 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,就放在靠外一层括号里面,第二层括号就保留最后的一个值
titanic=read.csv("https://goo.gl/4gqsnz") #从网络读取数据<0.2, 0.2-0.6还是>0.6。
目的:看不同舱位(pclass)和不同性别(x)的人的生存率是
a<- sort(unique(pclass)) #sort可以把类别按大小顺序排,unique()命令是把分类变量的种类提取出来b<- sort(unique(x))for(i in a){ for(j in b){ if(mean(survived[pclass==i&x==j])<0.2){ print(paste("for class",i,"x",j,"读高铁学校好吗mean surviv承翰学校al is less than 0.2")) } el if (mean(survived[pclass==i&x==j])>0.6){ print(paste("for class",i,"x",j,"mean survival is more than 0.6")) } el { print(paste("for class",i,"x",j,"mean survival is between 0.2 and 0.6"))} } }
结果如下:
[1] “for class 1 x female mean survival is more than 0.6”
[1] “for class 1 x male mean survival is between 0.2 and 0.6”
[1] “for class 2 x female mean survival is more than 0.6”
[1] “for class 2 x male mean survival is less than 0.2”
[1] “for class 3 x female mean survival is between 0.2 and 0.6”
[1] “for class 3 x male mean survival is less than 0.2”
补充:r语言for循环批量生成变量,并且赋值
rm(list=ls())data <- read.table("ms_identified_information.txt",header = t,p = "\t",quote="",na.strings = "",row.names = 1,comment.char = "")name1 <- paste("h1299",p = "_",c(1:3)给老师送什么礼物合适)name2 <- paste("metf",p = "_",c(1:3))name3 <- paste("oemetf",p = "_",c(1:3))name <- data.frame(name1,name2,name3)mean.data=data.frame(row.names(data))for (i in 1:3){ tmp <- subt(data,lect = 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, p=""), mean_)) //这里没有体现出变量,实际上生成了df1,df2,df3结果}colnames(mean.data) <- c("id","h1299","metf","oemetf")write.table(mean.data,file="ms_mean.xls",row.names = fal,p = "\t",na="")
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。
本文发布于:2023-04-09 02:55:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/39207fcf5d1afb7163f9fd255db2e48c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于R语言for循环的替换方案.doc
本文 PDF 下载地址:基于R语言for循环的替换方案.pdf
留言与评论(共有 0 条评论) |