0

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!

Amelia
  • 1
  • 1
  • The problem is the `dat$NTU` part. You've created `dat` to be an empty data frame in the function. It doesn't have a column named `NTU` so it doesn't matter what the threshold is. Plus, it's not a good idea to use `if` with vectors of length greater than one. You need to reduce to one truth value or loop over all the values. – MrFlick May 19 '15 at 05:19
  • @MrFlick - doesn't `dat` get overwritten later though? – thelatemail May 19 '15 at 05:20
  • 2
    Good point @thelatemail. Sounds like the file might not have an NTU column. (Though i'm still missing where `files` comes from) – MrFlick May 19 '15 at 05:21
  • 1
    You define an argument `data` for your function, but you don't use it inside the function. And you try to read `files[i]`, but as pointed out by @MrFlick, you don't define what is "files". –  May 19 '15 at 05:25
  • 1
    @pascal right, the OP should pass this in or create if inline, but it must defined somewhere otherwise read.csv would be throwing an error. Whatever is being read in, it doesn't have an NTU component. – Gavin Simpson May 19 '15 at 05:31

0 Answers0