0
我得到的錯誤是「錯誤'[.data.frame'(current_dataset,complete.cases(current_dataset)):未定義的列被選中」。我試圖找到問題,但無法弄清楚。在R中選擇了未定義的列
我想要的功能:首先,它是通過幾個文件,包含不同地點的硫酸鹽和硝酸鹽信息。這些文件都包含'csv',因此我的文件將被用作向量來輕鬆引用文件。然後我想遍歷332個文件,讀取它,並檢查是否有足夠的完整情況(這個數字是函數中的一個參數)。如果是這種情況,我想將所有完整的案例(硫酸鹽和硝酸鹽數據)添加到先前定義的數據框中。最後,我想返回硫酸鹽和硝酸鹽之間的關係。
corr <- function(directory, threshold = 0) {
#store data frame that holds sulfate amount and nitrate amount that meet threshold and are complete cases
data <- data.frame(sulfate = numeric(0), nitrate = numeric(0))
#set working directory
setwd(directory)
#get file names
myfiles <- list.files(pattern = "csv")
#loop through files
for(i in 1:332) {
#read each file
current_dataset <- read.csv(myfiles[i])
#check if there are enough compelte cases to meet threshold
if(sum(complete.cases(current_dataset)) > threshold) {
#get complete cases
complete_cases <- current_dataset[complete.cases(current_dataset)]
#add sulfate and nitrate info to table
data <- rbind(data, data.frame(sulfate = complete_cases$sulfate[i], nitrate = complete_cases$nitrate)[i])
}
}
#get correlation
cor(data)
}