Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

ProgrammingAssignment2

ProgrammingAssignment2

<title>coursera-r-programming/programming-assignment-2/README.md at master · edj-boston/coursera-r-programming</title>
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="GitHub" />
<link rel="fluid-icon" href="https://github.com/fluidicon.png" title="GitHub" />
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-114.png" />
<link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114.png" />
<link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-144.png" />
<link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144.png" />
<meta property="fb:app_id" content="1401488693436528"/>

  <meta content="@github" name="twitter:site" /><meta content="summary" name="twitter:card" /><meta content="edj-boston/coursera-r-programming" name="twitter:title" /><meta content="coursera-r-programming - Notes from the R programming Coursera course" name="twitter:description" /><meta content="https://avatars3.githubusercontent.com/u/2495522?s=400" name="twitter:image:src" />

<link rel="assets" href="https://assets-cdn.github.com/">
<link rel="conduit-xhr" href="https://ghconduit.com:25035">
<link rel="xhr-socket" href="/_sockets" />

<meta name="msapplication-TileImage" content="/windows-tile.png" />
<meta name="msapplication-TileColor" content="#ffffff" />
<meta name="selected-link" value="repo_source" data-pjax-transient />
  <meta name="google-analytics" content="UA-3769691-2">

<meta content="collector-new.githubapp.com" name="octolytics-host" /><meta content="collector-cdn.github.com" name="octolytics-script-host" /><meta content="github" name="octolytics-app-id" /><meta content="C12BF466:4C65:56127E2:5396BDAD" name="octolytics-dimension-request_id" /><meta content="7278335" name="octolytics-actor-id" /><meta content="gisontherun" name="octolytics-actor-login" /><meta content="a0e6d0ce41ffadb092493282097eaaec8ad5162cdf612990cffc096f583c6c2d" name="octolytics-actor-hash" />




<link rel="icon" type="image/x-icon" href="https://assets-cdn.github.com/favicon.ico" />


<meta content="authenticity_token" name="csrf-param" />
<link href="https://assets-cdn.github.com/assets/github-1aeb426322c64c12b92d56bda5b110fc1093f75e.css" media="all" rel="stylesheet" type="text/css" />
<link href="https://assets-cdn.github.com/assets/github2-b2cccfcac1a522b6ce675606f61388d36bf2c080.css" media="all" rel="stylesheet" type="text/css" />



<meta http-equiv="x-pjax-version" content="89c77479810e7a43a9a8c95a05773cca">

Skip to content
  <div class="header header-logged-in true">
<a class="header-logo-invertocat" href="https://github.com/">

<a href="/notifications" aria-label="You have no unread notifications" class="notification-indicator tooltipped tooltipped-s" data-hotkey="g n">
    <span class="mail-status all-read"></span>
  <div class="command-bar js-command-bar  in-repository">
      <form accept-charset="UTF-8" action="/search" class="command-bar-form" id="top_search_form" method="get">
data-username="gisontherun" data-repo="edj-boston/coursera-r-programming" data-branch="master" data-sha="9074d32752b67fd3f9c62c3e571391f27226d2b4"
<input type="hidden" name="nwo" value="edj-boston/coursera-r-programming" />

<div class="select-menu js-menu-container js-select-menu search-context-select-menu">
  <span class="minibutton select-menu-button js-menu-target" role="button" aria-haspopup="true">
    <span class="js-select-button">This repository</span>
  </span>

  <div class="select-menu-modal-holder js-menu-content js-navigation-container" aria-hidden="true">
    <div class="select-menu-modal">

      <div class="select-menu-item js-navigation-item js-this-repository-navigation-item selected">
        <span class="select-menu-item-icon octicon octicon-check"></span>
        <input type="radio" class="js-search-this-repository" name="search_target" value="repository" checked="checked" />
        <div class="select-menu-item-text js-select-button-text">This repository</div>
      </div> <!-- /.select-menu-item -->

      <div class="select-menu-item js-navigation-item js-all-repositories-navigation-item">
        <span class="select-menu-item-icon octicon octicon-check"></span>
        <input type="radio" name="search_target" value="global" />
        <div class="select-menu-item-text js-select-button-text">All repositories</div>
      </div> <!-- /.select-menu-item -->

    </div>
  </div>
</div>
  • New repository
  • New organization
  • <li class="section-title">
      <span title="edj-boston/coursera-r-programming">This repository</span>
    </li>
      <li>
        <a href="/edj-boston/coursera-r-programming/issues/new"><span class="octicon octicon-issue-opened"></span> New issue</a>
      </li>
    
  <div id="start-of-content" class="accessibility-aid"></div>
      <div class="site" itemscope itemtype="http://schema.org/WebPage">
<div id="js-flash-container">
  
</div>
<div class="pagehead repohead instapaper_ignore readability-menu">
  <div class="container">
    <li class="subscription">
      <form accept-charset="UTF-8" action="/notifications/subscribe" class="js-social-container" data-autosubmit="true" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="zEFz2xpj/7hAcda4we0nZNH0283lUEo8b9dyff4tr/cbwnYvUS6cP5k70+A4bZT+Y00VExCdGhKNQieklh2hSw==" /></div>  <input id="repository_id" name="repository_id" type="hidden" value="18561376" />
    
    <div class="select-menu js-menu-container js-select-menu">
      <a class="social-count js-social-count" href="/edj-boston/coursera-r-programming/watchers">
        1
      </a>
      <span class="minibutton select-menu-button with-count js-menu-target" role="button" tabindex="0" aria-haspopup="true">
        <span class="js-select-button">
          <span class="octicon octicon-eye"></span>
          Watch
        </span>
      </span>
    
      <div class="select-menu-modal-holder">
        <div class="select-menu-modal subscription-menu-modal js-menu-content" aria-hidden="true">
          <div class="select-menu-header">
            <span class="select-menu-title">Notification status</span>
            <span class="octicon octicon-x js-menu-close"></span>
          </div> <!-- /.select-menu-header -->
    
          <div class="select-menu-list js-navigation-container" role="menu">
    
            <div class="select-menu-item js-navigation-item selected" role="menuitem" tabindex="0">
              <span class="select-menu-item-icon octicon octicon-check"></span>
              <div class="select-menu-item-text">
                <input checked="checked" id="do_included" name="do" type="radio" value="included" />
                <h4>Not watching</h4>
                <span class="description">You only receive notifications for conversations in which you participate or are @mentioned.</span>
                <span class="js-select-button-text hidden-select-button-text">
                  <span class="octicon octicon-eye"></span>
                  Watch
                </span>
              </div>
            </div> <!-- /.select-menu-item -->
    
            <div class="select-menu-item js-navigation-item " role="menuitem" tabindex="0">
              <span class="select-menu-item-icon octicon octicon octicon-check"></span>
              <div class="select-menu-item-text">
                <input id="do_subscribed" name="do" type="radio" value="subscribed" />
                <h4>Watching</h4>
                <span class="description">You receive notifications for all conversations in this repository.</span>
                <span class="js-select-button-text hidden-select-button-text">
                  <span class="octicon octicon-eye"></span>
                  Unwatch
                </span>
              </div>
            </div> <!-- /.select-menu-item -->
    
            <div class="select-menu-item js-navigation-item " role="menuitem" tabindex="0">
              <span class="select-menu-item-icon octicon octicon-check"></span>
              <div class="select-menu-item-text">
                <input id="do_ignore" name="do" type="radio" value="ignore" />
                <h4>Ignoring</h4>
                <span class="description">You do not receive any notifications for conversations in this repository.</span>
                <span class="js-select-button-text hidden-select-button-text">
                  <span class="octicon octicon-mute"></span>
                  Stop ignoring
                </span>
              </div>
            </div> <!-- /.select-menu-item -->
    
          </div> <!-- /.select-menu-list -->
    
        </div> <!-- /.select-menu-modal -->
      </div> <!-- /.select-menu-modal-holder -->
    </div> <!-- /.select-menu -->
    
  • <form accept-charset="UTF-8" action="/edj-boston/coursera-r-programming/unstar" class="js-toggler-form starred" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="+r0MAAg9H7WBi+RqhDfx2dLaAhuZC1ZjjFck7TlrEAZLZ9dARvlFc4zVth0HqAFa3Y6bRdBxRct4g0S6Ia+MFA==" /></div>
      <button
        class="minibutton with-count js-toggler-target star-button"
        aria-label="Unstar this repository" title="Unstar edj-boston/coursera-r-programming">
        <span class="octicon octicon-star"></span><span class="text">Unstar</span>
      </button>
        <a class="social-count js-social-count" href="/edj-boston/coursera-r-programming/stargazers">
          0
        </a>
    
    Star 0
  •     <li>
          <a href="/edj-boston/coursera-r-programming/fork" class="minibutton with-count js-toggler-target fork-button lighter tooltipped-n" title="Fork your own copy of edj-boston/coursera-r-programming to your account" aria-label="Fork your own copy of edj-boston/coursera-r-programming to your account" rel="nofollow" data-method="post">
            <span class="octicon octicon-repo-forked"></span><span class="text">Fork</span>
          </a>
          <a href="/edj-boston/coursera-r-programming/network" class="social-count">8</a>
        </li>
    
    <h1 itemscope itemtype="http://data-vocabulary.org/Breadcrumb" class="entry-title public">
      <span class="repo-label"><span>public</span></span>
      <span class="mega-octicon octicon-repo"></span>
      <span class="author"><a href="/edj-boston" class="url fn" itemprop="url" rel="author"><span itemprop="title">edj-boston</span></a></span><!--
   --><span class="path-divider">/</span><!--
   --><strong><a href="/edj-boston/coursera-r-programming" class="js-current-repository js-repo-home-link">coursera-r-programming</a></strong>

      <span class="page-context-loader">
        <img alt="" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
      </span>

    </h1>
  </div><!-- /.container -->
</div><!-- /.repohead -->

<div class="container">
  <div class="repository-with-sidebar repo-container new-discussion-timeline js-new-discussion-timeline  ">
    <div class="repository-sidebar clearfix">
  • Code
  •     <li class="tooltipped tooltipped-w" aria-label="Issues">
          <a href="/edj-boston/coursera-r-programming/issues" aria-label="Issues" class="js-selected-navigation-item sunken-menu-item js-disable-pjax" data-hotkey="g i" data-selected-links="repo_issues /edj-boston/coursera-r-programming/issues">
            <span class="octicon octicon-issue-opened"></span> <span class="full-word">Issues</span>
            <span class='counter'>0</span>
            <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

      <li class="tooltipped tooltipped-w" aria-label="Pull Requests">
        <a href="/edj-boston/coursera-r-programming/pulls" aria-label="Pull Requests" class="js-selected-navigation-item sunken-menu-item js-disable-pjax" data-hotkey="g p" data-selected-links="repo_pulls /edj-boston/coursera-r-programming/pulls">
            <span class="octicon octicon-git-pull-request"></span> <span class="full-word">Pull Requests</span>
            <span class='counter'>0</span>
            <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

        <li class="tooltipped tooltipped-w" aria-label="Wiki">
          <a href="/edj-boston/coursera-r-programming/wiki" aria-label="Wiki" class="js-selected-navigation-item sunken-menu-item js-disable-pjax" data-hotkey="g w" data-selected-links="repo_wiki /edj-boston/coursera-r-programming/wiki">
            <span class="octicon octicon-book"></span> <span class="full-word">Wiki</span>
            <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

      <li class="tooltipped tooltipped-w" aria-label="Pulse">
        <a href="/edj-boston/coursera-r-programming/pulse" aria-label="Pulse" class="js-selected-navigation-item sunken-menu-item" data-pjax="true" data-selected-links="pulse /edj-boston/coursera-r-programming/pulse">
          <span class="octicon octicon-pulse"></span> <span class="full-word">Pulse</span>
          <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

      <li class="tooltipped tooltipped-w" aria-label="Graphs">
        <a href="/edj-boston/coursera-r-programming/graphs" aria-label="Graphs" class="js-selected-navigation-item sunken-menu-item" data-pjax="true" data-selected-links="repo_graphs repo_contributors /edj-boston/coursera-r-programming/graphs">
          <span class="octicon octicon-graph"></span> <span class="full-word">Graphs</span>
          <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

      <li class="tooltipped tooltipped-w" aria-label="Network">
        <a href="/edj-boston/coursera-r-programming/network" aria-label="Network" class="js-selected-navigation-item sunken-menu-item js-disable-pjax" data-selected-links="repo_network /edj-boston/coursera-r-programming/network">
          <span class="octicon octicon-repo-forked"></span> <span class="full-word">Network</span>
          <img alt="" class="mini-loader" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
    

          <div class="only-with-full-nav">

HTTPS clone URL

SSH clone URL

Subversion checkout URL

You can clone with HTTPS, SSH, or Subversion.

Clone in Desktop
            <a href="/edj-boston/coursera-r-programming/archive/master.zip"
               class="minibutton sidebar-button"
               aria-label="Download edj-boston/coursera-r-programming as a zip file"
               title="Download edj-boston/coursera-r-programming as a zip file"
               rel="nofollow">
              <span class="octicon octicon-cloud-download"></span>
              Download ZIP
            </a>
          </div>
    </div><!-- /.repository-sidebar -->

    <div id="js-repo-pjax-container" class="repository-content context-loader-container" data-pjax-container>

Permalink

Show File Finder

branch: master
<div class="select-menu-modal">
  <div class="select-menu-header">
    <span class="select-menu-title">Switch branches/tags</span>
    <span class="octicon octicon-x js-menu-close"></span>
  </div> <!-- /.select-menu-header -->

  <div class="select-menu-filters">
    <div class="select-menu-text-filter">
      <input type="text" aria-label="Filter branches/tags" id="context-commitish-filter-field" class="js-filterable-field js-navigation-enable" placeholder="Filter branches/tags">
    </div>
    <div class="select-menu-tabs">
      <ul>
        <li class="select-menu-tab">
          <a href="#" data-tab-filter="branches" class="js-select-menu-tab">Branches</a>
        </li>
        <li class="select-menu-tab">
          <a href="#" data-tab-filter="tags" class="js-select-menu-tab">Tags</a>
        </li>
      </ul>
    </div><!-- /.select-menu-tabs -->
  </div><!-- /.select-menu-filters -->

  <div class="select-menu-list select-menu-tab-bucket js-select-menu-tab-bucket" data-tab-filter="branches">

    <div data-filterable-for="context-commitish-filter-field" data-filterable-type="substring">


        <div class="select-menu-item js-navigation-item selected">
          <span class="select-menu-item-icon octicon octicon-check"></span>
          <a href="/edj-boston/coursera-r-programming/blob/master/programming-assignment-2/README.md"
             data-name="master"
             data-skip-pjax="true"
             rel="nofollow"
             class="js-navigation-open select-menu-item-text js-select-button-text css-truncate-target"
             title="master">master</a>
        </div> <!-- /.select-menu-item -->
    </div>

      <div class="select-menu-no-results">Nothing to show</div>
  </div> <!-- /.select-menu-list -->

  <div class="select-menu-list select-menu-tab-bucket js-select-menu-tab-bucket" data-tab-filter="tags">
    <div data-filterable-for="context-commitish-filter-field" data-filterable-type="substring">


    </div>

    <div class="select-menu-no-results">Nothing to show</div>
  </div> <!-- /.select-menu-list -->

</div> <!-- /.select-menu-modal -->
Fetching contributors…
<div class="participation">
  <p class="loader-loading"><img alt="" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32-EAF2F5.gif" width="16" /></p>
  <p class="loader-error">Cannot retrieve contributors at this time</p>
</div>
file 103 lines (83 sloc) 4.021 kb
        <a class="minibutton danger empty-icon tooltipped tooltipped-s"
           href="/edj-boston/coursera-r-programming/delete/master/programming-assignment-2/README.md"
           aria-label="Fork this project and delete file"
           data-method="post" data-test-id="delete-blob-file" rel="nofollow">

      Delete
    </a>
  </div><!-- /.actions -->
</div>

Introduction

This second programming assignment will require you to write an R function is able to cache potentially time-consuming computations. For example, taking the mean of a numeric vector is typically a fast operation. However, for a very long vector, it may take too long to compute the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of a vector are not changing, it may make sense to cache the value of the mean so that when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state inside of an R object.

Example: Caching the Mean of a Vector

In this example we introduce the <<- operator which can be used to assign a value to an object in an environment that is different from the current environment. Below are two functions that are used to create a special object that stores a numeric vector and cache's its mean.

The first function, makeVector creates a special "vector", which is really a list containing a function to

  1. set the value of the vector
  2. get the value of the vector
  3. set the value of the mean
  4. get the value of the mean
makeVector <- function(x = numeric()) {
        m <- NULL
        set <- function(y) {
                x <<- y
                m <<- NULL
        }
        get <- function() x
        setmean <- function(mean) m <<- mean
        getmean <- function() m
        list(set = set, get = get,
             setmean = setmean,
             getmean = getmean)
}

The following function calculates the mean of the special "vector" created with the above function. However, it first checks to see if the mean has already been calculated. If so, it gets the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function.

cachemean <- function(x, ...) {
        m <- x$getmean()
        if(!is.null(m)) {
                message("getting cached data")
                return(m)
        }
        data <- x$get()
        m <- mean(data, ...)
        x$setmean(m)
        m
}

Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and their may be some benefit to caching the inverse of a matrix rather than compute it repeatedly (there are also alternatives to matrix inversion that we will not discuss here). Your assignment is to write a pair of functions that cache the inverse of a matrix.

Write the following functions:

  1. makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
  2. cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not changed), then the cachesolve should retrieve the inverse from the cache.

Computing the inverse of a square matrix can be done with the solve function in R. For example, if X is a square invertible matrix, then solve(X) returns its inverse.

For this assignment, assume that the matrix supplied is always invertible.

In order to complete this assignment, you must do the following:

  1. Clone the GitHub repository containing the stub R files at https://github.com/gisontherun/ProgrammingAssignment2
  2. Edit the R file contained in the git repository and place your solution in that file (please do not rename the file).
  3. Commit your completed R file into YOUR git repository and push your git branch to your GitHub account.
  4. Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.

Grading

This assignment will be graded via peer assessment.

Jump to Line

Go
    </div>

  </div><!-- /.repo-container -->
  <div class="modal-backdrop"></div>
</div><!-- /.container -->
</div><!-- /.wrapper -->

  <div class="container">
  • Status
  • API
  • Training
  • Shop
  • Blog
  • About
  • </ul>
    
    <a href="/">
      <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
    </a>
    
    <ul class="site-footer-links">
      <li>&copy; 2014 <span title="0.09345s from github-fe117-cp1-prd.iad.github.net">GitHub</span>, Inc.</li>
        <li><a href="/site/terms">Terms</a></li>
        <li><a href="/site/privacy">Privacy</a></li>
        <li><a href="/security">Security</a></li>
        <li><a href="/contact">Contact</a></li>
    </ul>
    
<div class="fullscreen-overlay js-fullscreen-overlay" id="fullscreen_overlay">
<textarea name="fullscreen-contents" id="fullscreen-contents" class="fullscreen-contents js-fullscreen-contents" placeholder="" data-suggester="fullscreen_suggester"></textarea>
<div id="ajax-error-message" class="flash flash-error">
  <span class="octicon octicon-alert"></span>
  <a href="#" class="octicon octicon-x close js-ajax-error-dismiss"></a>
  Something went wrong with that request. Please try again.
</div>


  <script crossorigin="anonymous" src="https://assets-cdn.github.com/assets/frameworks-c044173f1e759e8299dbe414ec8ed23e4405bdc5.js" type="text/javascript"></script>
  <script async="async" crossorigin="anonymous" src="https://assets-cdn.github.com/assets/github-9e32825fbb6c4cc59498c59e87a67a26a4609e65.js" type="text/javascript"></script>
  
  
    <script async src="https://www.google-analytics.com/analytics.js"></script>

About

ProgrammingAssignment2

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors