Showing posts with label Data Science. Show all posts
Showing posts with label Data Science. Show all posts

Monday, August 10, 2015

R Programming - Matrix Inversion Cache. Solution

My submission:
makeCacheMatrix <- function(x = matrix()) {
        inverse <- NULL
 
        setMatrix <- function(matrix = matrix()){
                x <<- matrix
        }
 
        getMatrix <- function() x
 
        setInverse <- function(inverseMatrix = matrix()){ 
                inverse <<- inverseMatrix
        } 
        getInverse <- function() inverse
        list(get = getMatrix, set = setMatrix, getI = getInverse, setI = setInverse)
}
 
 
## checks if the given makeCacheMatrix object already has it's inverse calculate.
## If not, it calculates it's inverse and caches it
 
cacheSolve <- function(x, ...) {
        ## Return a matrix that is the inverse of 'x' 
 
        if(is.null(x$getI())){
                print("Not cached. Recomputing...") 
                x$setI(solve(x$get()))
        }else {print("Found in cache")}
 
        x$getI()
}

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)
                data <- read.csv(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="")
 
                data <- read.csv(path) 
                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="")
 
                data <- read.csv(path) 
                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
}