请在下面找到更清晰的代码。我很乐意回答任何问题。
corr <- function(directory, threshold = 0) {
# set the working directory
setwd(dir = directory)
# creates vector of filenames within the directory
spectdata <- list.files(pattern = ".csv")
# for each spectdata, read the sulfate and nitrate columns
L1 <- lapply(spectdata, function(x) read.csv(x, header = TRUE)[,c("sulfate","nitrate")])
# for each csv that was read, removes rows that have NA
L2 <- lapply(L1, function(x) x[complete.cases(x),])
# removes csv from list if not greater than or are equal to the threshold
L3 <- Filter(function(x) nrow(x) >= threshold, L2)
# if the list still has a csv results after Filter (length of list > 0) then:
if(length(L3) > 0) {
# for each csv in list, calculate and save correlation between sulfare and nitrate
Correlation <- lapply(L3, function(x) cor(x[,"sulfate"], x[,"nitrate"]))
# change list output to a vector output
unlist(Correlation)
} else {
# return a zero length vector
numeric(0)
}
}
corr(directory = "C:/Users/Evan Friedland/Desktop/DIRECTORY", threshold = 100)
corr <- function(directory, threshold = 0) {
setwd(dir = directory)
spectdata <- list.files(pattern = ".csv")
L1 <- lapply(spectdata, function(x) read.csv(x, header = TRUE)[,c("sulfate","nitrate")])
L2 <- lapply(L1, function(x) x[complete.cases(x),])
L3 <- Filter(function(x) nrow(x) >= threshold, L2)
if(length(L3) > 0) {
Correlation <- lapply(L3, function(x) cor(x[,"sulfate"], x[,"nitrate"]))
unlist(Correlation)
} else {
numeric(0)
}
}