I am very new to R and I am trying to write general functions instead of very specific ones for a certain file. I am trying to do a correlation for two variables at each site, only if one of the variables is above a certain threshold. My code looks like this:
turbidity<-function(data, Site = 1:3, threshold) {
dir <-file.path("~", "R")
dat <- data.frame()
corVect<- vector(mode = 'numeric')
for (i in Site) {
dat<-read.csv(files[i])
if (dat$NTU > threshold) {
correlation <- cor(dat$zscore, dat$NTU)
corVect <- c(corVect, correlation)
}
}
return(corVect)
}
turbidity("new.turbid", 1, 5)
And I get this error:
Error in if (dat$NTU > threshold) { : argument is of length zero.
When I change it to a number instead of threshold, I get the same thing:
turbidity<-function(data, Site = 1:3) {
dir <-file.path("~", "R")
dat <- data.frame()
corVect<- vector(mode = 'numeric')
for (i in Site) {
dat<-read.csv(files[i])
if (dat$NTU > 5) {
correlation <- cor(dat$zscore, dat$NTU)
corVect <- c(corVect, correlation)
}
}
return(corVect)
}
turbidity("new.turbid", 1)
Error in if (dat$NTU > 5) { : argument is of length zero
I would like be able to change the threshold on the command line of the function, but mostly, I really want it to work!
Any guidance would be super appreciated!