• R Programming - Week 3 Assignment
• by Ken Wood
• Last updated almost 3 years ago
• Hide Comments (–) Share Hide Toolbars

Work Harder.

## R Programming Week 3 Programming Assignment 2: Lexical Scoping

Introduction.

This second programming assignment will require you to write an R function that 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 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 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  get s 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 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.

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

Suggested Solution —

Tips for submitting assignment:

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

Love to help more and more people as I keep going on the Specialization Track! Hope this helps!

• Report this content
• Manage subscriptions
• Collapse this bar

## Programming Assignment 3: Hospital Quality

My code repository for coursera data science specialization by john hopkins university, assignment instructions.

The data for this assignment come from the Hospital Compare web site (http://hospitalcompare.hhs.gov) run by the U.S. Department of Health and Human Services. The purpose of the web site is to provide data and information about the quality of care at over 4,000 Medicare-certified hospitals in the U.S. This dataset es- sentially covers all major U.S. hospitals. This dataset is used for a variety of purposes, including determining whether hospitals should be fined for not providing high quality care to patients (see http://goo.gl/jAXFX for some background on this particular topic).

The Hospital Compare web site contains a lot of data and we will only look at a small subset for this assignment. The zip file for this assignment contains three files

• outcome-of-care-measures.csv: Contains information about 30-day mortality and readmission rates for heart attacks, heart failure, and pneumonia for over 4,000 hospitals.
• hospital-data.csv: Contains information about each hospital.
• Hospital_Revised_Flatfiles.pdf: Descriptions of the variables in each file (i.e the code book).

A description of the variables in each of the files is in the included PDF file named Hospital_Revised_Flatfiles.pdf. This document contains information about many other files that are not included with this programming assignment. You will want to focus on the variables for Number 19 (“Outcome of Care Measures.csv”) and Number 11 (“Hospital Data.csv”). You may find it useful to print out this document (at least the pages for Tables 19 and 11) to have next to you while you work on this assignment. In particular, the numbers of the variables for each table indicate column indices in each table (i.e. “Hospital Name” is column 2 in the outcome-of-care-measures.csv file)

## 2 Finding the best hospital in a state - best.R

Write a function called best that take two arguments: the 2-character abbreviated name of a state and an outcome name. The function reads the outcome-of-care-measures.csv file and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specified outcome in that state. The hospital name is the name provided in the Hospital.Name variable. The outcomes can be one of “heart attack”, “heart failure”, or “pneumonia”. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings.

## 3 Ranking hospitals by outcome in a state - rankhospital.R

Write a function called rankhospital that takes three arguments: the 2-character abbreviated name of a state (state), an outcome (outcome), and the ranking of a hospital in that state for that outcome (num). The function reads the outcome-of-care-measures.csv file and returns a character vector with the name of the hospital that has the ranking specified by the num argument. For example, the call rankhospital(“MD”, “heart failure”, 5) would return a character vector containing the name of the hospital with the 5th lowest 30-day death rate for heart failure. The num argument can take values “best”, “worst”, or an integer indicating the ranking (smaller numbers are better). If the number given by num is larger than the number of hospitals in that state, then the function should return NA. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings.

## 4 Ranking hospitals in all states - rankall.R

Write a function called rankall that takes two arguments: an outcome name (outcome) and a hospital ranking (num). The function reads the outcome-of-care-measures.csv file and returns a 2-column data frame containing the hospital in each state that has the ranking specified in num. For example the function call rankall(“heart attack”, “best”) would return a data frame containing the names of the hospitals that are the best in their respective states for 30-day heart attack death rates. The function should return a value for every state (some may be NA). The first column in the data frame is named hospital, which contains the hospital name, and the second column is named state, which contains the 2-character abbreviation for the state name. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings.

• Online Degree Explore Bachelor’s & Master’s degrees
• MasterTrack™ Earn credit towards a Master’s degree
• Top Courses

## This course is part of multiple programs

Instructors: Roger D. Peng, PhD +2 more

## Instructors

Instructor ratings

We asked all learners to give feedback on our instructors based on the quality of their teaching style.

Financial aid available

(22,101 reviews)

## Recommended experience

Intermediate level

Familiarity with regression is recommended.

## What you'll learn

Understand critical programming language concepts

Configure statistical programming software

Make use of R loop functions and debugging tools

Collect detailed information using R profiler

## Skills you'll gain

• Category: Computer Programming Computer Programming
• Category: Data Analysis Data Analysis
• Category: Data Structures Data Structures
• Category: Statistical Programming Statistical Programming
• Category: General Statistics General Statistics

## Details to know

0 quizzes, 6 assessments

Available in English

Subtitles: Kazakh, German, Hindi, Russian, Swedish, Korean, Portuguese (Brazilian), Greek, English, Italian, French, Chinese (Simplified), Spanish, Arabic, Vietnamese, Thai, Ukrainian, Japanese, Indonesian, Polish, Dutch, Turkish

## See how employees at top companies are mastering in-demand skills

• Learn new concepts from industry experts
• Gain a foundational understanding of a subject or tool
• Develop job-relevant skills with hands-on projects
• Earn a shareable career certificate

## Earn a career certificate

Share it on social media and in your performance review

## There are 4 modules in this course

In this course you will learn how to program in R and how to use R for effective data analysis. You will learn how to install and configure software necessary for a statistical programming environment and describe generic programming language concepts as they are implemented in a high-level statistical language. The course covers practical issues in statistical computing which includes programming in R, reading data into R, accessing R packages, writing R functions, debugging, profiling R code, and organizing and commenting R code. Topics in statistical data analysis will provide working examples.

## Week 1: Background, Getting Started, and Nuts & Bolts

This week covers the basics to get you started up with R. The Background Materials lesson contains information about course mechanics and some videos on installing R. The Week 1 videos cover the history of R and S, go over the basic data types in R, and describe the functions for reading and writing data. I recommend that you watch the videos in the listed order, but watching the videos out of order isn't going to ruin the story.

## What's included

28 videos 9 readings 1 quiz

## 28 videos • Total 129 minutes

• Installing R on a Mac • 1 minute • Preview module
• Installing R on Windows • 3 minutes
• Installing R Studio (Mac) • 1 minute
• Writing Code / Setting Your Working Directory (Windows) • 7 minutes
• Writing Code / Setting Your Working Directory (Mac) • 7 minutes
• Introduction • 1 minute
• Overview and History of R • 16 minutes
• Getting Help • 13 minutes
• R Console Input and Evaluation • 4 minutes
• Data Types - R Objects and Attributes • 4 minutes
• Data Types - Vectors and Lists • 6 minutes
• Data Types - Matrices • 3 minutes
• Data Types - Factors • 4 minutes
• Data Types - Missing Values • 2 minutes
• Data Types - Data Frames • 2 minutes
• Data Types - Names Attribute • 1 minute
• Data Types - Summary • 0 minutes
• Reading Tabular Data • 5 minutes
• Reading Large Tables • 7 minutes
• Textual Data Formats • 4 minutes
• Connections: Interfaces to the Outside World • 4 minutes
• Subsetting - Basics • 4 minutes
• Subsetting - Lists • 4 minutes
• Subsetting - Matrices • 2 minutes
• Subsetting - Partial Matching • 1 minute
• Subsetting - Removing Missing Values • 3 minutes
• Vectorized Operations • 3 minutes
• Introduction to swirl • 1 minute

## 9 readings • Total 90 minutes

• Welcome to R Programming • 10 minutes
• About the Instructor • 10 minutes
• Pre-Course Survey • 10 minutes
• Syllabus • 10 minutes
• Course Textbook • 10 minutes
• Course Supplement: The Art of Data Science • 10 minutes
• Data Science Podcast: Not So Standard Deviations • 10 minutes
• Getting Started and R Nuts and Bolts • 10 minutes
• Practical R Exercises in swirl Part 1 • 10 minutes

## 1 quiz • Total 30 minutes

• Week 1 Quiz • 30 minutes

## Week 2: Programming with R

Welcome to Week 2 of R Programming. This week, we take the gloves off, and the lectures cover key topics like control structures and functions. We also introduce the first programming assignment for the course, which is due at the end of the week.

13 videos 3 readings 2 quizzes

## 13 videos • Total 90 minutes

• Control Structures - Introduction • 0 minutes • Preview module
• Control Structures - If-else • 1 minute
• Control Structures - For loops • 4 minutes
• Control Structures - While loops • 3 minutes
• Control Structures - Repeat, Next, Break • 4 minutes
• Your First R Function • 10 minutes
• Functions (part 1) • 9 minutes
• Functions (part 2) • 7 minutes
• Scoping Rules - Symbol Binding • 10 minutes
• Scoping Rules - R Scoping Rules • 8 minutes
• Scoping Rules - Optimization Example (OPTIONAL) • 9 minutes
• Coding Standards • 8 minutes
• Dates and Times • 10 minutes

## 3 readings • Total 30 minutes

• Week 2: Programming with R • 10 minutes
• Practical R Exercises in swirl Part 2 • 10 minutes
• Programming Assignment 1 INSTRUCTIONS: Air Pollution • 10 minutes

## 2 quizzes • Total 60 minutes

• Week 2 Quiz • 30 minutes
• Programming Assignment 1: Quiz • 30 minutes

## Week 3: Loop Functions and Debugging

We have now entered the third week of R Programming, which also marks the halfway point. The lectures this week cover loop functions and the debugging tools in R. These aspects of R make R useful for both interactive work and writing longer code, and so they are commonly used in practice.

8 videos 2 readings 1 quiz

## 8 videos • Total 61 minutes

• Loop Functions - lapply • 9 minutes • Preview module
• Loop Functions - apply • 7 minutes
• Loop Functions - mapply • 4 minutes
• Loop Functions - tapply • 3 minutes
• Loop Functions - split • 9 minutes
• Debugging Tools - Diagnosing the Problem • 12 minutes
• Debugging Tools - Basic Tools • 6 minutes
• Debugging Tools - Using the Tools • 8 minutes

## 2 readings • Total 20 minutes

• Week 3: Loop Functions and Debugging • 10 minutes
• Practical R Exercises in swirl Part 3 • 10 minutes
• Week 3 Quiz • 30 minutes

## Week 4: Simulation & Profiling

This week covers how to simulate data in R, which serves as the basis for doing simulation studies. We also cover the profiler in R which lets you collect detailed information on how your R functions are running and to identify bottlenecks that can be addressed. The profiler is a key tool in helping you optimize your programs. Finally, we cover the str function, which I personally believe is the most useful function in R.

6 videos 4 readings 2 quizzes

## 6 videos • Total 42 minutes

• The str Function • 6 minutes • Preview module
• Simulation - Generating Random Numbers • 7 minutes
• Simulation - Simulating a Linear Model • 4 minutes
• Simulation - Random Sampling • 2 minutes
• R Profiler (part 1) • 10 minutes
• R Profiler (part 2) • 10 minutes

## 4 readings • Total 40 minutes

• Week 4: Simulation & Profiling • 10 minutes
• Practical R Exercises in swirl Part 4 • 10 minutes
• Programming Assignment 3 INSTRUCTIONS: Hospital Quality • 10 minutes
• Post-Course Survey • 10 minutes
• Week 4 Quiz • 30 minutes
• Programming Assignment 3: Quiz • 30 minutes

The mission of The Johns Hopkins University is to educate its students and cultivate their capacity for life-long learning, to foster independent and original research, and to bring the benefits of discovery to the world.

Specialization

Guided Project

## Learner reviews

Showing 3 of 22101

22,101 reviews

Reviewed on Feb 3, 2016

"R Programming" forces you to dive in deep.

These skills serve as a strong basis for the rest of the data science specialization. Material is in depth, but presented clearly. Highly recommended!

Reviewed on May 27, 2017

This was very engaging, however, the level of expectation and effort needed is much greater than course 1 - ToolBox.

This is perhaps the best course on R Programming designed for a small duration.

Reviewed on Aug 12, 2019

Very challenging, but good course. I've been programming in R for over a year, but there were still some things for me to pick up in this class. Assignments were a challenge, but satisfying to tackle.

## Open new doors with Coursera Plus

Earn a degree from world-class universities - 100% online

## Join over 3,400 global companies that choose Coursera for Business

Upskill your employees to excel in the digital economy

Access to lectures and assignments depends on your type of enrollment. If you take a course in audit mode, you will be able to see most course materials for free. To access graded assignments and to earn a Certificate, you will need to purchase the Certificate experience, during or after your audit. If you don't see the audit option:

The course may not offer an audit option. You can try a Free Trial instead, or apply for Financial Aid.

The course may offer 'Full Course, No Certificate' instead. This option lets you see all course materials, submit required assessments, and get a final grade. This also means that you will not be able to purchase a Certificate experience.

## What is the refund policy?

If you subscribed, you get a 7-day free trial during which you can cancel at no penalty. After that, we don’t give refunds, but you can cancel your subscription at any time. See our full refund policy Opens in a new tab .

## Is financial aid available?

Yes. In select learning programs, you can apply for financial aid or a scholarship if you can’t afford the enrollment fee. If fin aid or scholarship is available for your learning program selection, you’ll find a link to apply on the description page.

## More questions

#### IMAGES

1. GitHub

2. Coursers R programming week 3 Assignment Submission and Program

3. Basic R Programming

4. NPTEL: Programming ,Data Structures and Algorithms Using Python Week 3

5. R Programming Assignment Help UAE By Experts

6. R assignment #3 video

#### VIDEO

1. Week 7 Programming in Modern C++ #nptel #assignment #solved #swayam #education #shorts

2. NPTEL 2023| week 9| Assignment answer Problem solving through Programming in C|

3. 6. Week

4. NPTEL Programming In Java Week 9 Assignment 9 Answers Solution Quiz

5. NPTEL Getting Started With Competitive Programming Week 8 Programming Solution September 2023 IITG

6. NPTEL Programming in Modern C++ WEEK 4 ASSIGNMENT 4 ANSWERS Solutions Quiz

1. GitHub - PreetyHasija/R-Programming-Assignment-Week-3

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

2. Coursera R Programming Week 3 Assignment #1553 - GitHub

33 cachematrix.R @@ -1,15 +1,36 @@ ## Put comments here that give an overall description of what your ## functions do ## These functions written in partial fulfillment of Coursera Data Science: R Programming ## Week 3 Assignment; week beginning January 18, 2016; GitHub user: PamlaM ## Write a short comment describing this function

3. Coursers R programming week 3 Assignment Submission and ...

Coursers R programming week 3 Assignment Submission and Program Elakiya Ponmudi 126 subscribers 23K views 3 years ago This video contains the code for programming assignment-3 and the...

4. RPubs - R Programming - Week 3 Assignment

R Pubs by RStudio. Sign in Register R Programming - Week 3 Assignment; by Ken Wood; Last updated almost 3 years ago; Hide Comments (–) Share Hide Toolbars

5. R Programming Week 3 Programming Assignment 2: Lexical ...

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

6. GitHub: Let’s build from here · GitHub

7. Programming Assignment 3: Hospital Quality ...

This document contains information about many other files that are not included with this programming assignment. You will want to focus on the variables for Number 19 (“Outcome of Care Measures.csv”) and Number 11 (“Hospital Data.csv”). You may find it useful to print out this document (at least the pages for Tables 19 and 11) to have ...

8. R Programming, week (1-4) All Quiz Answers with Assignments.

course link: https://www.coursera.org/learn/r-programming?Friends support me to give you more useful videos.Subscribe me and comment me whatever courses you...

9. GitHub: Let’s build from here · GitHub