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