0
corr <- function(directory, threshold = 0){
#get all the cases that have non-NA values
complete_cases <- complete(directory)
#get all the observations over the threshold amount
filter_cases <- complete_cases[complete_cases[["nobs"]] > threshold, ]
#The returned data frame contains two columns "ID" and "nobs"
#get all file names in a vector
all_files <- list.files(directory, full.names=TRUE)
correlation <- vector("numeric")
for(i in as.numeric(filter_cases[["ID"]])){
#get all the files that are in the filter_cases
output <- read.csv(all_files[i])
#remove all NA values from the data
output <- output[complete.cases(output), ]
#get each of the correlations and store them
correlation[i] <- cor(output[["nitrate"]], output[["sulfate"]])
}
correlation
}
我预计将从这一点说就是这样的:[R矢量不打印预期输出
corr("directory", 200)
[1] -1.023 0.0456 0.8231 etc
我得到的是:
NA NA -1.023 NA NA
NA NA NA 0.0456 NA
0.8231 NA NA NA NA etc
我觉得这是一件简单的我作为print(cor(输出[[“硝酸盐”],输出[[“硫酸盐”]]))缺少基本上得到我所期望的。输出必须是一个向量,因为我打算在其他函数中使用该函数。
这正是问题所在。出于某种奇怪的原因,我仍然在我的数据中获得少数NA。我不确定这个新的代码为什么会是这种情况?当我输出“输出”变量时,那里没有NA,为什么相关性(cor)仍然有一些? – Shawn