2016-03-08 24 views
-1

我有很多問題,因爲我的if()聲明中沒有任何內容適用。我只是從一個單一的價值中獲得價值,或者根本沒有價值,而且我期望每次滿足我的條件時都會有一個具有多個值的向量。我的if()聲明中沒有任何內容

corr <- function(directory, threshold = 0, a = 0) { 
    y <- vector() 
    for(i in 332) { 
    x <- read.csv(paste("D:\\", directory, "\\", formatC(i, width = 3, flag = "0"), ".csv", sep = "")) 
    z <- x[complete.cases(x),] 
    n <- cor(z$sulfate,z$nitrate) 
    if(nrow(z) > threshold) { 
     a <- a + 1 
     y[a] <- n 
    } 
    } 
    return(y) 
} 
+2

請提供可重複的數據集。 –

+0

檢查[錯誤如果在R中的說法](http://stackoverflow.com/questions/32053436/error-in-if-statment-in-r) –

回答

2

重複的例子,就使這更容易,但一個問題是,你的for循環只運行一次。看一個簡單的例子

for (i in 332) { 
    print(i) 
} 

## [1] 332 

嘗試這種方式,這實際上將遍歷所有的值從1到332

for (i in seq_len(332)) { 
    ## the rest of your code here 
} 

或者

for (i in 1:332) { 
    ## the rest of your code here 
}