【问题标题】:R error : unexpected '}' in "}" I don't see where my code is going wrong :( [closed]R错误:“}”中的意外'}'我看不出我的代码哪里出错了:( [关闭]
【发布时间】:2013-06-14 17:01:40
【问题描述】:

我通常在函数上按运行,它们运行得很好。不幸的是,在这里我遇到了一个错误,我认为这是破坏我的整个代码的根源:S 任何建议都将不胜感激。附言我对 R 很陌生

代码:

CoefficientSorter=function(List1){
  for (p in 1: length(List1)){
    length(List1)
    XAxis=seq(from = 1, to =length(List1[[p]])/2 , by = 1) 
    ProperYvalues=rev(List1[[p]][,2])
    RegressionLine=lm(ProperYvalues~XAxis)
    Title= paste(ThreePeriodCounter, a[p])
    plot(XAxis,ProperYvalues, main=Title)
    abline(RegressionLine$coefficients)
    Coefficients=RegressionLine$coefficients
    names(Coefficients)=NULL
    if (Coefficients[2]<0){
      TemporaryVect=c(p, Coefficients[2])
      if (!exists("NegSlopeVect")){ NegSlopeVect= TemporaryVect}
      else{NegSlopeVect=rbind(NegSlopeVect, TemporaryVect, deparse.level=0)}
      rm(TemporaryVect)
    }
    else {
      if (Coefficients[2]>0){
        TemporaryVect=c(p, Coefficients[2])
        if (!exists("PosSlopeVect")){PosSlopeVect= TemporaryVect}
        else{
          PosSlopeVect=rbind(PosSlopeVect, TemporaryVect, deparse.level=0)
        }
        rm(TemporaryVect)
      }
    }  
    else {
      counter=counter+1
    }
    rm(RegressionLine)
    rm(Coefficients)
    rm(ProperYvalues)
  }

  TrialList2=list("PosSlopeVectR"=PosSlopeVect, "NegSlopeVect"=NegSlopeVect)
  return(TrialList2)

}

【问题讨论】:

  • 您的大括号在某处不匹配。如果您有一个可以为您匹配的编辑器,那应该可以帮助您找到它。如果您没有这样的编辑器,强烈考虑购买一个 - 从长远来看,它会对您有很大帮助。

标签: windows r statistics


【解决方案1】:

这不是 SO 的问题。你需要寻找一个更好的编辑器来为你提供语法高亮、括号匹配等。如果你是初学者,我推荐 RStudio。

另外,请在此处寻找替代方案:Recommendations for Windows text editor for R

有关您的代码出错的地方,请查看上面的@Andreas 解决方案。

【讨论】:

  • 第一次投反对票!好的! :)
  • 不投反对票。我刚刚撤回了我的投票,因为你改变了答案。
  • 啊,好吧!如果我将其添加回来,您会再次投票吗? JK。 :D
  • 当然,我赞成。