2016-03-08 46 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 
}