## Friday, August 7, 2015

### R Programming Week 2 Solutions

My solutions to Exercises:

#1 (pollutantmean.R)
```pollutantmean <- function(directory, pollutant, id=1:332){
sum <- 0
trueValues <- 0
for (i in id){
prefix <- "";
if (i<10)prefix <- "00"
else if(i<100) prefix <- "0"

path <- paste(directory, "\\", prefix, i, ".csv", sep="")
## print(path)
d <- data[[pollutant]]
d <- d[!is.na(d)]

# if(i==1)print(d)

sum <- sum + sum(d)

trueValues <- trueValues + sum(!is.na(d))
#                 if(i==1){
#                         print(sum(d))
#                         print(sum)
#                         print(trueValues)
#                         print(sum/trueValues)
#                 }
#         print(sum/length(id))
}
sum/trueValues
}```
#2 (complete.R)

```complete <- function(directory, id=1:332){
fileNos <- c()
numberOfElements <-c()
for (i in id){

prefix <- "";
if (i<10)prefix <- "00"
else if(i<100) prefix <- "0"

path <- paste(directory, "\\", prefix, i, ".csv", sep="")

d1 <- data[["sulfate"]]
d2 <- data[["nitrate"]]

good <- complete.cases(d1, d2)
bothNotNA <- length(good[good==TRUE])
fileNos <- c(fileNos, i)
numberOfElements <- c(numberOfElements, bothNotNA)
}
data.frame(id = fileNos, nobs = numberOfElements)
}```
#3 (corr.R)

```corr <- function(directory, threshold = 0){
cors <- c()
for (i in 1:332){

prefix <- "";
if (i<10)prefix <- "00"
else if(i<100) prefix <- "0"

path <- paste(directory, "\\", prefix, i, ".csv", sep="")

d1 <- data[["sulfate"]]
d2 <- data[["nitrate"]]

good <- complete.cases(d1, d2)
bothNotNA <- length(good[good])
if(bothNotNA>threshold){
cors <- c(cors, cor(d1[good], d2[good]))
}
}
if(length(cors)==0)vector(mode="numeric", length=0)
else cors
}```