R Programming Week 3 Programming Assignments 2: Lexical Scoping

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.

Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and there 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

Write the following functions:

makeCacheMatrix: This function creates a special “matrix” object that can cache its inverse. Answer:

A pair of functions that cache the inverse of a matrix.

This function creates a special “matrix” object that can cache its inverse..

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. Answer:

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

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

Fork the GitHub repository containing the stub R files to create a copy under your own account. Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine. Edit the R file contained in the git repository and place your solution in that file (please do not rename the file). Commit your completed R file into YOUR git repository and push your git branch to the GitHub repository under your account. Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment. In addition to submitting the URL for your GitHub repository, you will need to submit the 40 character SHA-1 hash (as string of numbers from 0-9 and letters from a-f) that identifies the repository commit that contains the version of the files you want to submit. You can do this in GitHub by doing the following:

Going to your GitHub repository web page for this assignment Click on the “?? commits” link where ?? is the number of commits you have in the repository. For example, if you made a total of 10 commits to this repository, the link should say “10 commits”. You will see a list of commits that you have made to this repository. The most recent commit is at the very top. If this represents the version of the files you want to submit, then just click the “copy to clipboard” button on the right hand side that should appear when you hover over the SHA-1 hash. Paste this SHA-1 hash into the course web site when you submit your assignment. If you don’t want to use the most recent commit, then go down and find the commit you want and copy the SHA-1 hash. A valid submission will look something like (this is just an example!)

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing 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.

makeCacheMatrix: This function creates a special “matrix” object that can cache its inverse. 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 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.

Suggested Solution —

##Please include your own comment to explain your code (Required in Rubric)

#Tips for submitting assignment:

  • Download GitHub Desktop
  • Fork the GitHub repository containing the stub R files to create a copy under your own account.
  • Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine. Clone or Download, revise it in R or Rstudio
  • Commit(or Copy) your completed R file into YOUR git repository and push your git branch to the GitHub repository under your account. *Or you can do pull request on Github.
  • Submit with the URL to your GitHub repository that contains the completed R code for the assignment. Remember to include SHA-1 hash identifier in your submission.

R Programming Assignment 2: Lexical Scoping

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 caches its mean.

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

  • set the value of the vector
  • get the value of the vector
  • set the value of the mean
  • get the value of the mean

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.

Assignment: Caching the Inverse of a Matrix

Matrix inversion is usually a costly computation and there 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:

  • makeCacheMatrix: This function creates a special “matrix” object that can cache its inverse.
  • 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.

My Solution

cachematrix.R:

Testing My Functions

Another useful example.

A good example for getting a better understanding of differences amount formal parameters, local variables and free variables . And it is also helpful for learning the <<- operator.

open.account.R:

Simple tests for open.account function:

More information about Scope .

xmuxiaomo

  • 1. Introduction
  • 2. Example: Caching the Mean of a Vector
  • 3. Assignment: Caching the Inverse of a Matrix
  • 4.1. Functions
  • 4.2. Testing My Functions
  • 5. Another Useful Example
  • Programming Assignment 2: Lexical Scoping JHU
  • by [email protected]
  • Last updated almost 4 years ago
  • Hide Comments (–) Share Hide Toolbars

Twitter Facebook Google+

Or copy & paste this link into an email or IM:

Instantly share code, notes, and snippets.

@arnavsin390

arnavsin390 / Week 3 R assignment

  • Download ZIP
  • Star ( 0 ) 0 You must be signed in to star a gist
  • Fork ( 7 ) 7 You must be signed in to fork a gist
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.
  • Learn more about clone URLs
  • Save arnavsin390/cd5be50d555ffa31bd8bc250ce8aec4d to your computer and use it in GitHub Desktop.

IMAGES

  1. Programming Assignment 2 Lexical Scoping Coursera Instructions

    programming assignment 2 lexical scoping answer

  2. GitHub

    programming assignment 2 lexical scoping answer

  3. Programming Assignment 2 Lexical Scoping Coursera Instructions

    programming assignment 2 lexical scoping answer

  4. Programming Assignment 2 Lexical Scoping Coursera Instructions

    programming assignment 2 lexical scoping answer

  5. GitHub

    programming assignment 2 lexical scoping answer

  6. Solved Probem 2

    programming assignment 2 lexical scoping answer

VIDEO

  1. **UPDATED** AP CS A

  2. CS304 Object Oriented Programming Assignment 2 Fall 2023 Virtual University of Pakistan

  3. [SOLVED] CSE340 Project 2: Parsing

  4. Lexical Analysis Part 2 Attributes of Token Lexical Errors

  5. 7.What are the important points of arrow functions in JavaScript #weekendcodingintelugu

  6. 💓scopin #shortvideo #music #subscribe #tranding 😱#funny #dj#dhun #youtubeshort #viral

COMMENTS

  1. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping

    Week 3 programming assignment_b.r 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.

  2. Peer-graded-Assignment-Programming-Assignment-2-Lexical-Scoping

    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 caches its mean.

  3. R Programming Week 3 Programming Assignments 2: Lexical Scoping

    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 ...

  4. R Programming Assignment 2: Lexical Scoping

    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 ...

  5. Peer-graded-Assignment-Programming-Assignment-2-Lexical-Scoping

    Saved searches Use saved searches to filter your results more quickly

  6. R Programming Week 3 Programming Assignments 2: Lexical Scoping

    RPubs - R Programming Week 3 Programming Assignments 2: Lexical Scoping. R Programming Week 3 Programming Assignments 2: Lexical Scoping. by Louis Stanley. Last updated over 1 year ago.

  7. RPubs

    RPubs - Programming Assignment 2: Lexical Scoping JHU. by [email protected]. almost 4 years ago.

  8. PDF Peer-graded Assignment: Programming Assignment 2: Lexical Scoping

    Back to Week 3 Lessons This Course : R Programming Prev Next less less Discussions Peer-graded Assignment: Programming Assignment 2: Lexical Scoping You passed! Congratulations. You earned 12 / 12 points. Instructions My submission second programming assignment will require you to write an R function is able to cache

  9. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping

    Download ZIP. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping -- for Douglas Rubin, BMS. Raw. Week 3 programming assignment_b.r. 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.

  10. On coursera R-programming course Assignment 2 Lexical Scoping

    Each variable has 2 functions set and get. So, set is a function that takes an argument and do something inside it, in this code set takes y as an argument (dummy variable could be any other name) and assign it to the matrix x. The same for inv. This will make the only access for these variables is through set and get.

  11. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping · GitHub

    Week 3 R assignment 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.

  12. Lexical Scoping: Understanding Its Role in Programming

    Lexical scoping is a fundamental concept in programming that governs how variables are accessed and scoped. By understanding its principles and implications, developers can write cleaner, more ...

  13. R Programming Assignment 2: Lexical Scoping #1556

    Here is my submission of the programming assignment 2 in R for Caching the Inverse of a Matrix. ... Assignment: Programming Assignment 2: Lexical Scoping reetikas Jan 19, 2016. File filter Filter by extension. Filter by extension.R (1) All 1 file type selected Viewed files Clear filters ...

  14. Peer-graded-Assignment-Programming-Assignment-2-Lexical-Scoping

    In this Programming Assignment you 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 ...

  15. R Programming (JHU Coursera, Course 2)

    R Programming JHU Quiz 1. Week 2 Highlights: Lexical scoping as the reason why all objects must be stored in memory. Programming assignment is useful. As seen below, I have decided to do as much of the specialization in data.table syntax as possible since it is widely used in industry.

  16. Coursera course "R Programming"

    For the Coursera course R Programming The following code demonstrates how to use the assessment3.R R script. Lines starting with # are simple comments, lines starting with #> are things printed in the output.

  17. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping · GitHub

    Week 3 programming assignment_b.r 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.

  18. Peer-graded Assignment: Programming Assignment 2: Lexical Scoping · GitHub

    Week 3 R assignment 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.

  19. NivaanVedante/Programming-Assignment-2-Lexical-Scoping

    Write better code with AI Code review. Manage code changes

  20. a33596/Peer-graded-Assignment-Programming-Assignment-2-Lexical-Scoping-

    100.0%. Contribute to a33596/Peer-graded-Assignment-Programming-Assignment-2-Lexical-Scoping- development by creating an account on GitHub.