为此,R 无需命令行即可擦除对象

R erasing objects without command line for this

我的代码有这个问题:当我 运行 所有脚本时,我得到错误 Error: object 'i' not foundError: object 'scp.L50' not found。数据框和de counter i被无序擦除,之后(显然)R无法找到scp.L50并在其上添加信息。但是,我不知道为什么,当我 运行 只是代码的一部分(没有 #bla bla bla)时,它工作正常。 几天前所有代码都运行良好,但现在我遇到了这个问题。我已经尝试更新 R(光滑的人行道以充满成分)但仍然无法正常工作。有人能帮我吗? 谢谢!

代码如下:

library("minpack.lm", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
library("gdata", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")

sex<-c("FEMALE","MALE","ALL")
files <- list.files(pattern = "txt")
scp.L50<-data.frame()

for (i in 1:length(files)){
  dados<-read.table(files[i],header=TRUE,fill = TRUE)

  dados<-na.exclude(dados)
  dados<-dados[order(dados[,1]),]
  rownames(dados)<-1:nrow(dados)

  species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
  if (ncol(dados)==3){
  dadosF<-dados[(which(dados[,3]=="F",arr.ind=TRUE)),]; dadosM<-dados[(which(dados[,3]=="M",arr.ind=TRUE)),]; dados.total<-dados

  if (length(dadosF[,1])!=0){
    dadosF<-dadosF[order(dadosF[,1]),]
  } else {
    rm(dadosF)}

  if (length(dadosM[,1])!=0){
    dadosM<-dadosM[order(dadosM[,1]),]
  } else {
    rm(dadosM)}}

  if(ncol(dados)==3){
  for (gender in sex){
    try({
    if (gender=="FEMALE") {
      dados<-dadosF
     }},silent=TRUE)
    try({
    if (gender=="MALE"){
      dados<-dadosM
    }},silent=TRUE)
    try({
    if (gender=="ALL"){
      dados<-dados.total
     }},silent=TRUE)

dados<-dados[,-3]

par(mfrow=c(1,1))

if (dados[1,1]==0) {
  dados<-dados[-1,]}

unifitted<-nlsLM(Weight~a*Length^b,
                 data=dados,
                 start=list(a=0.1,b=3),
                 weights=wfct(fitted),
                 control=nls.lm.control(maxiter=1024))

coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
sd.dados<-sd(dados[,3])

rownames(dados)<-1:nrow(dados)

sd.dados<-sd(dados[,3])
  for (j in nrow(dados):1){
    if (dados[j,3]/sd.dados > 2.5){dados<-dados[-j,]
    }else{
      if (dados[j,3]/sd.dados < -2.5){dados<-dados[-j,]}
  }}

interval.cons(25)
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)

rownames(dados)<-1:nrow(dados)

dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999

polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
                      data=dados.1.3,
                      start=list(a=0.01,b=3),
                      control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))

polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
                      data=dados.3.3,
                      start=list(a=0.01,b=3),
                      control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))

scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2

for (scp.loop in scp.matrix){
  for (fit.rate in 1:10){
    try({
      polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) + 
                                (a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
                              data=dados,
                              start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
                              lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
                              upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
                              control=nls.lm.control(maxiter=1024)) 

      coeff.poly<-as.vector(coef(polyfitted.total))
      polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) + 
                                                 (coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
      polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) + 
        ((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))

      polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) + 
                                      (coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
      polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) + 
        ((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))

      dados<-dados[,-5:-6]
      dataLC<-dataLC[,-5:-6]
      dados<<-cbind(dados,polyfitted.resid,polyfitted)
      dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)

      phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
      polyfitted.phase1.res<-dados[1:phases,5]
      polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
      polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
      polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])

      r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)

      if(sum(resid(polyfitted.total)^2)<sum.resid){
        if(r.squared.test>r.squared.lr){
          print(">> Best combination found <<")
          best.loop<<-polyfitted.total
          sum.resid<-sum(resid(polyfitted.total)^2)
          rate<<-fit.rate
          scp<<-scp.loop
          r.squared.test<-r.squared.lr
          coeff.poly<<-as.vector(coef(polyfitted.total))
        }}else{coeff.poly.only.res<<-coef(polyfitted.total)}
    },silent=FALSE)}}

sd.dados<-sd(dados[,5])
rownames(dados)<-1:nrow(dados)

for (j in nrow(dados):1){
  if (dados[j,5]/sd.dados > 1.96){dados<-dados[-j,]
  }else{
    if (dados[j,5]/sd.dados < -1.96){dados<-dados[-j,]}
  }}

rownames(dados)<-1:nrow(dados)
rm(j,sd.dados)

keep(dados,dadosF,dadosM,dados.total,interval.cons,files,sex,gender,sure=T)


if (nrow(dados)<150){next}

cut.point<-round((nrow(dados)*0.025),digits=0)
dados<-dados[-1:-cut.point,]
dados<-dados[(nrow(dados)-cut.point:nrow(dados)),]

dados<-dados[order(dados[,1]),]
rownames(dados)<-1:nrow(dados)

print(paste("Calculating ", gender, " data: ",length(dados[,1])," points"))

interval.cons(25)
dados<-dados[,-3:-6]

unifitted<-nlsLM(Weight~a*Length^b,
                data=dados,
                start=list(a=0.1,b=3),
                weights=wfct(fitted),
                control=nls.lm.control(maxiter=1024))

coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])

unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)

Linf<-exp(0.044 + 0.9841*log(max(dados[,1])))
if (gender=="FEMALE"){
  L50<-exp(0.9469*log(Linf) - 0.1162)
} 
if (gender=="MALE"){
  L50<-exp(0.8915*log(Linf) - 0.1032)
}
if(gender=="ALL"){
  L50<-exp(0.8979*log(Linf) - 0.0782)
}

dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999

polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
                      data=dados.1.3,
                      start=list(a=0.01,b=3),
                      control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))

polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
                      data=dados.3.3,
                      start=list(a=0.01,b=3),
                      control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))

scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2

for (scp.loop in scp.matrix){
  for (fit.rate in 1:10){
    try({
      polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) + 
                                (a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
                              data=dados,
                              start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
                              lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
                              upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
                              control=nls.lm.control(maxiter=1024)) 

      coeff.poly<-as.vector(coef(polyfitted.total))
      polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) + 
                                                 (coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
      polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) + 
        ((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))

      polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) + 
                                      (coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
      polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) + 
        ((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))

      dados<-dados[,-5:-6]
      dataLC<-dataLC[,-5:-6]
      dados<<-cbind(dados,polyfitted.resid,polyfitted)
      dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)

      phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
      polyfitted.phase1.res<-dados[1:phases,5]
      polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
      polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
      polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])

      r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)

      if(sum(resid(polyfitted.total)^2)<sum.resid){
        if(r.squared.test>r.squared.lr){
          print(">> Best combination found <<")
          best.loop<<-polyfitted.total
          sum.resid<-sum(resid(polyfitted.total)^2)
          rate<<-fit.rate
          scp<<-scp.loop
          r.squared.test<-r.squared.lr
          coeff.poly<<-as.vector(coef(polyfitted.total))
        }}else{coeff.poly.only.res<<-coef(polyfitted.total)}
    },silent=FALSE)}}

coeff.poly<-as.vector(coef(best.loop))
polyfitted.total.rate.scp<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - fit.scp)))) + 
                                    (a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - fit.scp))))),
                                  data=dados,
                                  start=list(a1=coeff.poly[[1]],b1=coeff.poly[[2]],a2=coeff.poly[[3]],b2=coeff.poly[[4]],fit.rate=rate,fit.scp=scp),
                                  lower=c(a1=coeff.poly[[1]]-coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]-coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]-coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]-coeff.poly[[4]]*0.5,fit.rate=rate-(rate*0.2),fit.scp=(scp-(scp*0.2))),
                                  upper=c(a1=coeff.poly[[1]]+coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]+coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]+coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]+coeff.poly[[4]]*0.5,fit.rate=rate+(rate*0.2),fit.scp=(scp+(scp*0.2))),
                                  weights=wfct(fitted),
                                  control=nls.lm.control(maxiter=1024))

coeff.poly.rate.scp<-as.vector(coef(polyfitted.total.rate.scp))

polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))) + 
                                           (coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))) + 
  ((coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))))

polyfitted.resid<-(dados[,2]-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))) + 
                                   (coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))))))/dados[,2]
polyfitted<-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))) + 
  ((coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))))

dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
coeff.poly<-as.data.frame(coef(summary(polyfitted.total.rate.scp))[,1:2])

phases<-apply(dataLC,2,function(x) length(x[x<=coeff.poly[6,1]]));phases<-phases[-2:-6]
unifitted.phase1.res<-dataLC[,3]; unifitted.phase1.res<-unifitted.phase1.res[1:phases];
unifitted.phase2.res<-dataLC[,3]; unifitted.phase2.res<-unifitted.phase2.res[(phases+1):nrow(dataLC)]
unifitted.phase1.lr<-lm(unifitted.phase1.res~dataLC[1:phases,1]); unifitted.phase2.lr<-lm(unifitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])

polyfitted.phase1.res<-dataLC[1:phases,5]
polyfitted.phase2.res<-dataLC[(phases+1):nrow(dataLC),5]
polyfitted.phase1.lr<-lm(polyfitted.phase1.res~dataLC[1:phases,1])
polyfitted.phase2.lr<-lm(polyfitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])

graph.name<-paste(files[i],gender,sep=" ");graph.name<-gsub(".txt", "", graph.name);graph.name<-gsub("_", " ", graph.name)

a.values<-paste("a = ",round(coeff.uni[1,1],digits=4)," (se: ",round(coeff.uni[1,2],digits=4),")",sep="")
b.values<-paste("b = ",round(coeff.uni[2,1],digits=4)," (se: ",round(coeff.uni[2,2],digits=4),")",sep="")

a1.values<-paste("a = ",round(coeff.poly[1,1],digits=4)," (se: ",round(coeff.poly[3,2],digits=4),")",sep="")
b1.values<-paste("b = ",round(coeff.poly[2,1],digits=4)," (se: ",round(coeff.poly[4,2],digits=4),")",sep="")
a2.values<-paste("a = ",round(coeff.poly[3,1],digits=4)," (se: ",round(coeff.poly[1,2],digits=4),")",sep="")
b2.values<-paste("b = ",round(coeff.poly[4,1],digits=4)," (se: ",round(coeff.poly[2,2],digits=4),")",sep="")
scp.values<-paste("SCP = ",round(coeff.poly[6,1],digits=2)," (se: ",round(coeff.poly[6,2],digits=2),")",sep="")
rate.values<-paste("rate = ",round(coeff.poly[5,1],digits=3)," (se: ",round(coeff.poly[5,2],digits=3),")",sep="")
L50.values<-paste("L   = ",round(L50,digits=2),sep="")

par(mfrow=c(2,2))
plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({
lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)

plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,4],lwd=3)
mtext(a.values,line=-1.5,adj=0.05,cex=.6)
mtext(b.values,line=-2.4,adj=0.05,cex=.6)

plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
abline(v=coeff.poly[6,1],lty=2:2)
try({
lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
abline(v=L50,lty=3)

plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,6],lwd=3)
abline(v=coeff.poly[6,1],lty=2:2)
abline(v=L50,lty=3)

mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.06,cex=.35)

par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)

mypath <- file.path("~/Documents/Doutorado/Dados R/Gráficos",paste(graph.name, ".pdf", sep = ""))
mypath.rdata <- file.path("~/Documents/Doutorado/Dados R/Resultados novos",paste(graph.name, ".Rdata", sep = ""))

save(list=ls(all=TRUE),file=mypath.rdata)

pdf(file=mypath)
  par(mfrow=c(2,2))
  plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
  points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({  
  lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
  abline(h=0,lty=2:2)

  plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
  lines(dados[,1],dados[,4],lwd=3)
  mtext(a.values,line=-1.5,adj=0.05,cex=.6)
  mtext(b.values,line=-2.4,adj=0.05,cex=.6)

  plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
  points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
  abline(v=coeff.poly[6,1],lty=2:2)
try({  
  lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)  
  abline(h=0,lty=2:2)
  abline(v=L50,lty=3)

  plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
  lines(dados[,1],dados[,6],lwd=3)
  abline(v=coeff.poly[6,1],lty=2:2)
  abline(v=L50,lty=3)

mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.05,cex=.35)

par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)
dev.off()

scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
scp.L50<-rbind(scp.L50,scp.L50.paste)
  }}
names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
write.csv(scp.L50,file="Summary.csv")

该代码有 43 个左括号和 42 个右括号,因此如果您将其作为脚本执行,则不能期望 运行 正确。我不确定你丢失的括号应该在哪里,因为这取决于你想做什么,但你应该仔细看看并尝试找到它。看看你最近的变化,你是不是忘记了闭环之类的?

我检查了你的代码,确实少了一个括号。假设您的代码缩进是正确的,并且您只想写一次摘要,那么我认为这就是问题所在。请注意,我已经在我发表评论的行中添加了右括号:

for (i in 1:length(files)){ # !! I believe you forgot to close this bracket !!
  # Some code here..
  species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
  if (ncol(dados)==3){
    # Some more code here..
    }

  if(ncol(dados)==3){
    for (gender in sex){
      # Wall of code
      scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
      scp.L50<-rbind(scp.L50,scp.L50.paste)
    }}
  } # !! I believe your closing bracket should be added here !!
  names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
  write.csv(scp.L50,file="Summary.csv")

编辑:换句话说,它很可能是最外层的 for 循环没有正确关闭。