【发布时间】:2014-06-15 21:14:41
【问题描述】:
我得到的错误是“'[.data.frame'(current_dataset, complete.cases(current_dataset)) 中的错误:选择了未定义的列”。我试图找到问题,但不能'想不通。
我希望该函数执行的操作: 首先,它会检查几个包含不同位置的硫酸盐和硝酸盐信息的文件。这些文件都包含“csv”,因此 myfiles 将用作向量以轻松引用文件。然后我想遍历 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)
}
【问题讨论】:
标签: r