Last active
July 23, 2018 15:37
-
-
Save byanuaria/035f0f6bbbc3fd67f03849f03282be63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # problem 1 | |
| # part 1 | |
| pollutantmean <- function(directory, pollutant, id = 1:332) { | |
| filelist <- list.files(path = directory, pattern = ".csv", | |
| full.names = TRUE) | |
| values <- numeric() | |
| for(i in id) { | |
| data <- read.csv(filelist[i]) | |
| values <- c(values, data[[pollutant]]) # sulfate or nitrate | |
| } | |
| mean(values, na.rm = TRUE) | |
| } | |
| pollutantmean("specdata/", "nitrate", 70:72) | |
| # part 2 | |
| complete <- function(directory, id = 1:332) { | |
| filelist <- list.files(path = directory, pattern = ".csv", | |
| full.names = TRUE) | |
| # create empty vectors to later be cbind'd into a data frame | |
| ide <- numeric() | |
| nobs <- numeric() | |
| for(i in id) { | |
| # reads the data directory | |
| data <- read.csv(filelist[i]) | |
| # id numbers chosen gets put into the vector | |
| ide <- c(ide, id[i]) | |
| # finds all the values that are not NA and assigns to nobs vector | |
| nobs <- c(nobs, sum(!is.na(data[[2]]))) | |
| } | |
| data.frame(cbind(id, nobs)) # vectors now with data, changed to DF | |
| } | |
| # part 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test