R Programming Week 3 Assignment

For large square matrices, it may take too long to compute the inverse, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of the matrix do not change, it may make sense to cache the matrix inverse so that, when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment, we take advantage of the scoping rules of the R language and observe how they can be manipulated to preserve state inside of an R object.

For this assignment, 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 we will use to create a special object that stores a square matrix and caches its inverse.

The first function, makeCacheMatrix creates a special “matrix”, which is really a list containing a function to:

  • set the elements of the matrix
  • get the elements of the matrix
  • set the elements of the matrix inverse
  • get the elements of the matrix inverse

The following function calculates the inverse of the special “matrix” created with the above function. However, it first checks to see if the inverse has already been calculated. If so, it gets the inverse from the cache and skips the computation. Otherwise, it calculates the inverse of the matrix and sets it in the cache via the setinverse function.

First example to test my code…

Second example to test my code…

VSA R Training (Fall 2022)

Week 3 Project Assignment

This lesson is called Week 3 Project Assignment, part of the VSA R Training (Fall 2022) course. This lesson is called Week 3 Project Assignment, part of the VSA R Training (Fall 2022) course.

If the video is not playing correctly, you can watch it in a new window

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

For this week's assignment we're asking you to read a dataset into your RMarkdown report and to do something with it – we don't mind what!

If you're using your own .csv file you'll need to add it to the Gist (shown in the video).

If you're using a TidyTuesday dataset please include the code you used (also shown in the video!)

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login .

Course Content

142 Lessons

R Programming

Welcome back.

r programming week 3 assignment

Programming Assignment 1 Review

  • In-class Walkthrough 5

Dealing with Missing Data

  • Specialness of NAs 7
  • You Should Care About NA s 8
  • Strategy 1: Total Eradication 9
  • Strategy 2: Handle on Subsets 10
  • Strategy 3: Imputation 11

Visualizing Data

  • Base Plotting System 13-17
  • Other Plotting Systems in R 18
  • Graphics Devices 19

Combining and Transforming Data Frames

  • Columnwise Combination 21-22
  • Rowwise Combination 23-24

Final Project Discussion

  • Project Proposal Guidelines 26
  • Final Project Outline 27

Assignment 1 Discussion

IMHO, this assignment was the single hardest thing you'll be asked to do in this class.

r programming week 3 assignment

Specialness of NAs

NA is a special object in R, used to capture the idea of "a value whose value is unknown". Confusing, right? They're an inevitability in real-world data.

PRO TIP : See ?NA for R's documentation on the nuances of NA

You Should Care About NAs

It's common for introductory programmers to think of missing values as problems to address, but that isn't always the case! NA can actually hold valuable information. For example, imagine that you get a dump of data from Salesforce or some other CRM system with information like customer_name, date_of_first_contact, and date_of_second_contact.

Depending on how the system was set up, date_of_second_contact1 may have dates only for customers who have been contacted at least twice, and be NA everywhere else. This is valuable information! If you want to build a model of 1-contact conversion, you could use the presence/absence of NA to help you identify the 1-contact customers that belong in your model.

Strategy 1: Total Eradication

The first approach you may take to dealing with NA values is to simply drop them from your data. If you don't think these missing data have any business value and your dataset is big enough that you can afford to drop some rows / columns, this is the right move for you.

Strategy 2: Handle on Subsets

You may find the "remove all the NAs everywhere" strategy a bit too aggressive for your use case. If you have a 100-variable dataset and a single variable (column) is 90\% NA values, do you really want to drop every row where that variable is NA? A better approach might be to selectively subset out columns where missing values are most severe before using complete.cases() to remove rows.

Strategy 3: Imputation

A final strategy, particularly useful in modeling contexts, is to use some imputation strategy to replace NA values with reasonable alternatives. One common approach (and my favorite), the roughfix method. It works like this:

  • For numeric columns, replace NAs with the column median
  • For categorical columns, replace NAs with the most common value

Base Plotting System

R's built-in plotting tools, called "the base plotting system", is one of its most popular features.

The essential idea of the base plotting system is to build up plots in layers. You first create a simple plot, then "add on" a legend, more variables, other plot types, etc.

See "The Base Plotting System" in the programming supplement.

Creating a Scatter Plot

Let's start with a simple scatter plot to answer the question are sepal length and sepal width related?

r programming week 3 assignment

Histograms and Densities

Histograms and densities are useful for examining distributions.

r programming week 3 assignment

Multi-variable line charts

You can add more than one variable to these plots!

r programming week 3 assignment

Creating a Grid of plots

You can combine multiple plots in a grid layout. See "The Base Plotting System" for an example.

r programming week 3 assignment

Other Plotting Systems in R

We don't have time in this short class to go into great depth on data visualization, but I want you to know that there are a bunch of cool visualization libraries a short install.packages() away!

  • dygraphs : high-level library for creating interactive charts that can be embedded directly in HTML
  • ggplot2 : One of the most popular packages in the R world. Based on the "grammar of graphics" approach to building plots
  • googleVis : Send your data to the google charts API to make fancy interactive visualizations
  • plotly : easy-to-use library for creating interactive data visualizations and dashboards

A Note On Graphics Devices

When R (or any other program!) creates plots, it needs to know where to put them! When you call plot() or other commands from within and RStudio session, the default is to just display the resulting figure in the "Plots" pane. However, you can use other graphics devices (places to put visual output) to tell R to put your figures elsewhere.

r programming week 3 assignment

Columnwise Combination With cbind

In situations where you have multiple data frames with the same rows but different columns, you can combine them column-wise with R's cbind() command. Note that this command will only work if the two data frames to be joined have the same number of rows AND those rows refer to the same observation.

cbind = "column-wise bind"

r programming week 3 assignment

Column Matching with merge

It's common in data science workflows to have two mismatched tables of data from different sources and to want to combine them by matching on one or more keys. Think JOIN in SQL or VLOOKUP in Excel. To perform this operation in R, you can use the merge() command.

r programming week 3 assignment

Rowwise Combination With rbind

So far we've talked about merging columns from different tables. But what if you want to merge rows? For example, imagine that you are a researcher in a lab studying some natural phenomenon. You may take multiple samples (measuring the same variables) and then want to put them together into a single data frame to build a model. For this case, we can use R's rbind() function.

rbind = "row-wise bind"

r programming week 3 assignment

Rowwise Combination of Many Tables with rbindlist

What if you have 5 tables? 10? 1000? Use data.table::rbindlist() .

r programming week 3 assignment

Your Final Project Proposal is Due in Week 4

Choosing External Packages

  • You need to choose one data retrieval package, one statistics package, and one visualization package from this list
  • Change which packages you actually use in the final project
  • Use a package that isn't on the list (as long as you clear it with me)
  • Use more than just 3 external packages

What Your Proposal Should Cover

  • What data set do you plan to use? Where can others find it? What variables does it contain?
  • What is the question you're trying to answer?
  • What packages do you plan to use?

Your Final Project is Due in Week 5

  • let's go through the Final Project description

r programming week 3 assignment

Additional Resources

Plotting in R : graphics devices

Paths: Relative vs absolute | listing files in a directory in R

Instantly share code, notes, and snippets.

@kfeoktistoff

kfeoktistoff / best.R

  • Download ZIP
  • Star ( 2 ) 2 You must be signed in to star a gist
  • Fork ( 26 ) 26 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 kfeoktistoff/cc127819122f404156f9 to your computer and use it in GitHub Desktop.
best <- function(state, outcome) {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with lowest 30-day death
## rate
source("sortHospitalsByOutcome.R")
head(sortHospitalsByOutcome(state, outcome), 1)
}
outcomeCol <- function(outcome) {
if (outcome == "heart attack") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"
} else if (outcome == "heart failure") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
} else if (outcome == "pneumonia") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"
}
else {
stop("invalid outcome")
}
}
rankall <- function(outcome, num="best") {
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
source("outcomeCol.R")
outcome <- outcomeCol(outcome)
data <- read.csv("data/outcome-of-care-measures.csv", colClasses="character")
data[,outcome] <- suppressWarnings(as.numeric(data[,outcome]))
data <- data[order(data$"State", data[outcome], data$"Hospital.Name", na.last=NA),]
data <- data[!is.na(outcome)]
l <- split(data[,c("Hospital.Name")], data$State)
rankHospitals <- function(x, num) {
if (num=="best") {
head(x, 1)
} else if (num=="worst") {
tail(x, 1)
} else {
x[num]
}
}
result <- lapply(l, rankHospitals, num)
data.frame(hospital = unlist(result), state = names(result), row.names = names(result))
}
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with the given rank
## 30-day death rate
source("best.R")
source("sortHospitalsByOutcome.R")
if (num=="best") {
best(state, outcome)
} else if (num=="worst") {
tail(sortHospitalsByOutcome(state, outcome), 1)
} else {
sortHospitalsByOutcome(state, outcome)[num]
}
}
sortHospitalsByOutcome <- function(state, outcome) {
source("outcomeCol.R")
outcome <- outcomeCol(outcome)
data <- read.csv("data/outcome-of-care-measures.csv", stringsAsFactors=FALSE)
if (!state %in% data$State) {
stop("invalid state")
}
data <- data[data$State==state,]
data[,outcome] <- suppressWarnings(as.numeric(data[,outcome]))
data <- data[order(data[outcome], data$"Hospital.Name"),]
as.character(data$"Hospital.Name"[!is.na(data[outcome])])
}

@Cosmin11

Cosmin11 commented Mar 21, 2015

best <- function(state, outcome ) {

Read outcome data----------------------------------------

Error msg for outcomes-----------------------------------, error msg for state--------------------------------------, create data frame with hosp., state, h. attack, h. failure & pneumonia-----, replace 'not available with na---------------------------------------------, find minimum value of the outcome--------------------------------------------, display all hospitals with the minimum dead rate-----------------------------, display result in alphabetical ordered------------------------------------------, display first value from the result vector-------------------------------------.

Sorry, something went wrong.

@bbrewington

bbrewington commented Jul 10, 2015

You should take this down, since you're enabling other people to copy it and cheat. Also, this goes against the Coursera Honor Code (link: https://www.coursera.org/about/terms/honorcode )

It says "I will not make solutions to homework, quizzes, exams, projects, and other assignments available to anyone else (except to the extent an assignment explicitly permits sharing solutions). This includes both solutions written by me, as well as any solutions provided by the course staff or others."

@rahul351987

rahul351987 commented Feb 15, 2016

Good one but calling functions in other codes makes it difficult plus time consuming!

  • Programming Assignment 3 - R Programming
  • by Wagner Pinheiro
  • Last updated over 7 years ago
  • Hide Comments (–) Share Hide Toolbars

Twitter Facebook Google+

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

IMAGES

  1. Coursers R programming week 3 Assignment Submission and Program

    r programming week 3 assignment

  2. Coursera R Programming Week 3 Assignment by neerjachandra05 · Pull

    r programming week 3 assignment

  3. R programming Week 3 Part 3 simple vectorized arithmetic

    r programming week 3 assignment

  4. R Programming Week 3 Quiz Answer

    r programming week 3 assignment

  5. Coursera Data Analysis with R Programming

    r programming week 3 assignment

  6. R programming Week 3 Part 8 Using output as input and the linear

    r programming week 3 assignment

COMMENTS

  1. GitHub

    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. However, for a very long vector, it may take too long to compute the mean, especially ...

  2. RPubs

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

  3. Coursers R programming week 3 Assignment Submission and Program

    This video contains the code for programming assignment-3 and the step by step instructions for submission of assignment which includes forking, cloning repo...

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

    R Programming Week 3 Programming Assignments 2: Lexical Scoping ... 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 ...

  5. Coursera R Programming Week 3 Assignment #1553

    Original file line number Diff line number Diff line change @@ -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

  6. Peer-graded Assignment: Programming Assignment 2: Lexical ...

    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. Learn more about bidirectional Unicode characters. Show hidden characters ...

  7. R Programming Week 3 Assignment

    R Programming Week 3 Assignment Ken Wood 7/6/2020. ... In this Programming Assignment, we take advantage of the scoping rules of the R language and observe how they can be manipulated to preserve state inside of an R object. For this assignment, we introduce the <<- operator which can be used to assign a value to an object in an environment ...

  8. R Programming

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

  9. Week 3 Project Assignment

    Week 3 Project Assignment. This lesson is called Week 3 Project Assignment, part of the VSA R Training (Fall 2022) course. This lesson is called Week 3 Project Assignment, part of the VSA R Training (Fall 2022) course. Get Lifetime Access $0.00 If the video is not playing correctly, you can watch it in a new window . ...

  10. R Programming

    Week 1 Quiz • 30 minutes. 7 programming assignments • Total 1,260 minutes. swirl Lesson 1: Basic Building Blocks • 180 minutes. swirl Lesson 2: Workspace and Files • 180 minutes. swirl Lesson 3: Sequences of Numbers • 180 minutes. swirl Lesson 4: Vectors • 180 minutes. swirl Lesson 5: Missing Values • 180 minutes.

  11. assignment of R programming week 3 by coursera

    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. Grading. This assignment will be graded via peer assessment.

  12. R Programming Quiz 3 (Week 3) John Hopkins Data Science ...

    There will be an object called 'iris' in your workspace. In this dataset, what is the mean of 'Sepal.Length' for the species virginica? (Please only enter the numeric result and nothing else.)

  13. sinawebgard/Programming-Assignment-3

    The R script, FUN.R contains three stand-alone functions which download and read data before arrange it in a specific order defined by the assignment instructions. Please note that if you are doing this assignment as part of your Coursera course you are asked to save each function in a stand-alone R file.

  14. R Programming

    R's built-in plotting tools, called "the base plotting system", is one of its most popular features. The essential idea of the base plotting system is to build up plots in layers. You first create a simple plot, then "add on" a legend, more variables, other plot types, etc. See "The Base Plotting System" in the programming supplement.

  15. R Programming

    We also introduce the first programming assignment for the course, which is due at the end of the week. What's included. 13 videos 3 readings 2 quizzes 3 programming assignments. Show info about module content. ... We have now entered the third week of R Programming, which also marks the halfway point. The lectures this week cover loop ...

  16. R Programming

    R Pubs. by RStudio. Sign in Register. Ryan Tillis - R Programming - Data Science - Quiz 3 - Coursera. by Ryan Tillis. Last updated almost 8 years ago. Comments (-)

  17. R Programming Programming Assignment 3 (Week 4) John Hopkins Data

    And this course makes me do this for every assignment!!!!! By the way, I have a decent knowledge of programming where I gain from learning Python. I thought this class would be easy for me (after quickly going through the lecture videos), yet from the first assignment I began to scratch my head for an answer.

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

    R Pubs. by RStudio. Sign in Register. R Programming Week 3 Programming Assignments 2: Lexical Scoping. by Louis Stanley. Last updated almost 2 years ago. Comments (-)

  19. Programming assignment 3 for Coursera "R Programming" course by Johns

    Star 2 2. Fork 26 26. Programming assignment 3 for Coursera "R Programming" course by Johns Hopkins University. Raw. best.R. best <- function (state, outcome) {. ## Read outcome data. ## Check that state and outcome are valid. ## Return hospital name in that state with lowest 30-day death.

  20. RPubs

    R Pubs. by RStudio. Sign in Register. [Programming Assignment 3] R Programming. by Anderson Hitoshi Uyekita. Last updated over 2 years ago.

  21. Programming Assignment 3: Quiz >> R Programming

    Programming Assignment 3: Quiz >> R Programming 1. What result is returned by the following code? best ("SC", "heart attack") CAROLINAS HOSPITAL SYSTEMMCLEOD MEDICAL CENTER - DARLINGTONMUSC MEDICAL CENTERMARY BLACK MEMORIAL HOSPITALLAKE CITY COMMUNITY HOSPITALGRAND STRAND REG MED CENTER 2. What result is returned by the following code? best ...

  22. GitHub

    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.

  23. RPubs

    RPubs - Programming Assignment 3 - R Programming. R Pubs. by RStudio. Sign in Register. Programming Assignment 3 - R Programming. by Wagner Pinheiro. Last updated over 7 years ago.