首页 > 解决方案 > 由于意外的“}”而出错 - R 中的问题。如何解决?

问题描述

for (i in 1:214)
{
  temp1 <- subset(perm2, perm2$isin == list[1+i])
  temp2 <- subset(temp1, temp1$Price != "NA")
  tempstart <- min(temp2$Time)
 if (is.na(tempstart)) 
   {
tempstart = "01/01/2020" else tempstart = tempstart
    }
 temp3 <- perm2[perm2$isin == list[1=i] & perm2$Time >= tempstart]
 perm3 <- rbind(perm3,temp3)
}

我正在编写一个将一堆数据绑定在一起的函数。不幸的是,我的一些数据不起作用,我需要指定一个日期来观察日期。

但后来我得到了意想不到的}问题>>“错误:“}”中的意外'}'”

谁能解释一下?

谢谢你。

标签: rdatabaseloops

解决方案


The if condition loop also wraps the else inside, instead it should end followed by else

 tempstart = "01/01/2020" else tempstart = tempstart

would be

 tempstart = "01/01/2020" 
  } else {tempstart <- tempstart}

Without a small reproducible example, it is not clear about the structure of the objects. Considering the "list" as list and NA as real NA,

for(i in 1:214){ 
    temp1 <- subset(perm2, isin == list[[1+i]] & !is.na(Price))
    tempstart <- min(temp1$Time)
    if(is.na(tempstart)) {
       tempstart <- "01/01/2020"
     }
    temp3 <- perm2[perm2$isin == list[[i-i]] & perm2$Time >= tempstart,]
    perm3 <- rbind(perm3,temp3)

 }

Also, the i + 1, assumes that the list have length of 215 or else when i is 214, there won't be corresponding element for list


推荐阅读