Doubt about conditional (if) in function

3

I have a set of 300 and few spreadsheets in which we have to create a function with 3 arguments: the directory where the spreadsheets are, the variable that will be analyzed and the amount of files to analyze.

In the case in question we have two variables of interest, the sulfate concentration and the nitrate concentration.

I have been able to equate the function for two parameters, in which I will return the average sulfate and separately the average nitrate.

Follow the code:

pollutant_sulfate<-function(directory, ID = 1:332) {
    files_list <- list.files(directory, full.names=TRUE)
    data <- data.frame()
    for (i in ID) {
            data <- rbind(data, read.csv(files_list[i]))
    }
    subset_sulfate<- subset(data$sulfate, data$sulfate > 0)
    mean (subset_sulfate)
}

pollutant_nitrate<-function(directory, ID = 1:332) {
    files_list <- list.files(directory, full.names=TRUE)
    data <- data.frame()
    for (i in ID) {
            data <- rbind(data, read.csv(files_list[i]))
    }
    subset_nitrate<- subset(data$nitrate, data$nitrate > 0)
    mean (subset_nitrate)
}
Now the 3rd argument of the function that would be the determination of which variable I wish to analyze (sulfate or nitrate) I am in difficulties. I thought about building a if condition. I wrote a code that contains errors and I can not understand what the problem is. Here is the code in question:

mean_pollutant1<-function(directory, pollutant, ID=1:332){
    files_list <- list.files(directory, full.names=TRUE)
    data <- data.frame()
    for (i in ID) {
        data <- rbind(data, read.csv(files_list[i]))
    }
    if (pollutant == sulfate){
        subset_sulfate<- subset(data$sulfate, data$sulfate > 0)
        mean (subset_sulfate)   
    }
    if (pollutant == nitrate){
        subset_nitrate<- subset(data$nitrate, data$nitrate > 0)
        mean (subset_nitrate)
    }
}

When I try to call the function I get error msg:

  

mean_pollutant1 ("specdata", sulfate, 1: 2)   Error in mean_pollutant1 ("specdata", sulfate, 1: 2):     object 'sulfate' not found

Can anyone help me get around the problem?

    
asked by anonymous 10.11.2014 / 19:46

1 answer

3

The problem with your code is that you are comparing objects that do not exist.

In if (pollutant == sulfate) , the sulfate object does not exist. You can solve the problem in two ways:

  • Before if create the object, assigning the value of a string, ie put before if a sulfate <- 'sulfate'
  • Compare directly with string 'sulfate' , if (pollutant == 'sulfate') .
  • This you should do for the two if 's.

    The other problem with your code is that you are missing a return . In R , it is possible to return values of a function without using return , but only if the value returned is the last command of the function.

    In your case, the last command of the function is

    if (pollutant == 'nitrate'){
        subset_nitrate<- subset(data$nitrate, data$nitrate > 0)
        mean (subset_nitrate)
    }
    

    So any string other than 'nitrate' will return NULL . To solve this, just put a return() in each if. So the correct function would be:

    mean_pollutant1<-function(directory, pollutant, ID=1:332){
        files_list <- list.files(directory, full.names=TRUE)
        data <- data.frame()
        for (i in ID) {
            data <- rbind(data, read.csv(files_list[i]))
        }
        if (pollutant == 'sulfate'){
            subset_sulfate<- subset(data$sulfate, data$sulfate > 0)
            return(mean(subset_sulfate))
        }
        if (pollutant == 'nitrate'){
            subset_nitrate<- subset(data$nitrate, data$nitrate > 0)
            return(mean (subset_nitrate))
        }
    }
    

    Of course there are other outputs to avoid having to use return , but this is the one that least modifies your original code.

        
    13.11.2014 / 10:52