Created
February 16, 2015 04:52
-
-
Save eayoungs/7239ea7652ceb078ae66 to your computer and use it in GitHub Desktop.
R Data Frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pollutantmean <- function(directory, pollutant, id = 1:332) { | |
## 'directory' is a character vector of length 1 indicating | |
## the location of the CSV files | |
dir_vect <- as.character(directory) | |
polltnt_vect <- as.character(pollutant) | |
valid_files <- list.files(path = dir_vect, pattern = "\\.csv$") | |
if(length(valid_files) > 0) { | |
i = 1 | |
full_path <- paste(dir_vect, valid_files[i], sep = "") | |
data_file <- file.path(full_path, fsep = .Platform$file.sep) | |
file_data <- read.csv(data_file) | |
dir_data <- file_data[complete.cases(file_data),][,] | |
for(i in 2:length(valid_files)) { | |
full_path <- paste(dir_vect, valid_files[i], sep = "") | |
data_file <- file.path(full_path, fsep = .Platform$file.sep) | |
file_data <- read.csv(data_file) | |
valid_data <- file_data[complete.cases(file_data),][,] | |
dir_data <- rbind(dir_data, valid_data) | |
} | |
} else{ | |
print("No CSV files found") | |
return | |
} | |
## 'pollutant' is a character vector of length 1 indicating | |
## the name of the pollutant for which we will calculate the | |
## mean; either "sulfate" or "nitrate". | |
if(polltnt_vect == "sulfate" | polltnt_vect == "nitrate") { | |
## 'id' is an integer vector indicating the monitor ID numbers | |
## to be used | |
if(id > 0 && id < 333) { | |
slctd_Data <- subset(dir_data, ID == id, select=c(polltnt_vect, "ID")) | |
print(slctd_Data) | |
print(mean(slctd_Data)) | |
} else { | |
print("Invalid pollutant argument") | |
} | |
## Return the mean of the pollutant across all monitors list | |
## in the 'id' vector (ignoring NA values) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment