我不确切知道发生了什么,因为当我在提示符下以简单的方式尝试我想做的事情时,它工作正常,但是当我在我的功能中执行它时,它根本就没有。工作。
这是简单的版本
p
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6 7
7 7 8
8 8 9
9 9 10
10 10 11
> p[,2][y!=2]
[1] 3 4 5 6 7 8 9 10 11
这是我的功能
corr <- function(directory, threshold = 0) {
#setwd(directory)
# Correlations of all the monitors
corr_all_monit <- data.frame(id = 1:332, corrs = 0)
for(i in 1:332){
# Reading the files in the directory
idi <- paste(formatC(i, width=3, flag="0"), "csv", sep=".")
onedataset <- read.csv(idi)
# Eliminating the NAs
onedataset <- onedataset[complete.cases(onedataset),]
# Getting the length of the filtered data
monitor_length <- length(onedataset[,1])
# Doing the correlation of the data which surpass the threshold
if(monitor_length > threshold){
corr_all_monit[i,2] <- cor(onedataset[,2],onedataset[,3])
}
}
# Picking up only the correlations which surpass the threshold
return(corr_all_monit[,2][corrs != 0])
}
当我尝试运行它时,我得到:
source("corr.R")
corr("/Users/Fanaro/specdata",150)
Error in corr("/Users/Fanaro/specdata", 150) : object 'corrs' not found