Librarian - CS50's Introduction to Programming with R (2024)

OpenCourseWare

Donate

Carter Zenke
carter@cs50.harvard.edu
GitHubLinkedIn

David J. Malan
malan@harvard.edu
FacebookGitHubInstagramLinkedInRedditThreadsTwitter

Librarian - CS50's Introduction to Programming with R (2)

From Afterplace

Problem to Solve

You have tended to a vast library at the edge of the woods for many years, helping readers find the knowledge they need to continue on their journey.

In a folder named library, in files named log.R and answers.txt, help several readers find the books they are looking for.

Distribution Code

For this problem, you’ll need to download log.R, answers.txt, and two CSV files: books.csv and authors.csv.

Download the distribution code

Open RStudio per the linked steps and navigate to the R console:

>

Next execute

getwd()

to print your working directory. Ensure your current working directory is where you’d like to download this problem’s distribution code. If using RStudio through cs50.dev the recommended directory is /workspaces/NUMBER where NUMBER is a number unique to your codespace.

If you do not see the right working directory, use setwd to change it! Try typing setwd("..") if in the working directory of another problem, which will move you one directory higher.

Next execute

download.file("https://cdn.cs50.net/r/2024/x/psets/2/librarian.zip", "librarian.zip")

in order to download a ZIP called librarian.zip into your codespace.

Then execute

unzip("librarian.zip")

to create a folder called librarian. You no longer need the ZIP file, so you can execute

file.remove("librarian.zip")

Now type

setwd("librarian")

followed by Enter to move yourself into (i.e., open) that directory. Your working directory should now end with

librarian/

If all was successful, you should execute

list.files()

and see answers.txt, authors.csv, books.csv, and log.R. If not, retrace your steps and see if you can determine where you went wrong!

Schema

Before jumping in, it will be helpful to get a sense for the “schema” (i.e., organization!) of the data you’re given.

Learn about this data

Provided to you are two CSV files: books.csv and authors.csv.

In books.csv, you’ll find that each row is a book in your library. For each book, you have the following information:

  • title, which is the title of the book
  • author, which is the author of the book
  • topic, which is the topic of the book
  • year, which is the year the book was published
  • pages, which is the number of pages in the book

In authors.csv, you’ll find that each row is an author of a book in your library. For each author, you have the following information:

  • author, which is the name of the author
  • hometown, which is the hometown of the author

Specification

For this problem, equally as important as finding the books is the process you use to do so. In log.R, keep a log of all R code you run to find each reader’s book. Label each section with a comment: in R, comments are any lines that begin with #, per the below:

# This is a comment in R

The comment should describe why you’ve written the code you’ve written, as well as what information you’re hoping to glean from it. Ultimately, log.R should serve as evidence of the process you used to find each book!

Upon finding a book, complete the corresponding line in answers.txt by filling in the book’s title. Be sure not to change any of the existing text in the file or to add any other lines to the file!

See a sample completed answers.txt file
The Writer is looking for: Navigating the Literary MarketThe Musician is looking for: The Art of Piano PlayingThe Traveler is looking for: Elemental ConjurationsThe Painter is looking for: The Abstract RevolutionThe Scientist is looking for: The Diversity of BirdsThe Teacher is looking for: Fostering a Sense of Belonging

Ultimately, you should submit both your log.R and answers.txt files.

The Writer

Your first reader of the day walks up to your desk and greets you:

Dearest librarian, curator, cataloger! I yearn, hunger, dream for the author Mia Morgan’s sole, only, exclusive book! Please uncover it, fetch it, retrieve it for me. I will owe you a great debt, a wonderful sum, an immortal obligation.

In log.R, write R code to find the title of the book that The Writer is referencing. Once you’ve found the title, write it in answers.txt.

The Musician

A reader with a ukulele walks up to your desk:

Hey, I’m on the lookout for a book on the topic of music, a real classic. I think it hit the shelves back in 1613. Music history is just so fascinating, don’t you think?

In log.R, write R code to find the title of the book that The Musician is referencing. Once you’ve found the title, write it in answers.txt.

The Traveler

A hunched, cloaked figure approaches your desk, handing you a sheet of paper without saying a word. On it, you see two possible author names: Lysandra Silverleaf or Elena Petrova. Below, you see the year in which the book was published: 1775.

In log.R, write R code to find the title of the book that The Traveler is referencing. Once you’ve found the title, write it in answers.txt.

The Painter

From behind, you hear a reader call to you:

Oh, I remember this wonderful book on the topic of art from my childhood! It was like a burst of colors—vivid reds, soothing blues, vibrant yellows. It was not too long, not too short, probably between 200 and 300 pages. And it was definitely published in either 1990 or 1992, but absolutely not 1991.

In log.R, write R code to find the title of the book that The Painter is referencing. Once you’ve found the title, write it in answers.txt.

The Scientist

You receive a phone call:

I need the book with “Quantum Mechanics” in the title.

In log.R, write R code to find the title of the book that The Scientist is referencing. Once you’ve found the title, write it in answers.txt.

Hint

The function grepl determines—TRUE or FALSE—whether a pattern of characters is present in another character string. For instance, consider the following:

grepl("Biology", "The Biology of Coral Reefs")

which would return:

TRUE

grepl can also be used with vectors:

grepl("Biology", c("The Biology of Coral Reefs", "The Biology of Plants", "Marine Conservation"))

The above would return a vector of length three:

TRUE TRUE FALSE

The Teacher

A small knock on the door reveals your next reader:

Apologies for the trouble, but I’m looking for a book on the topic of education published in the 1700s. Unfortunately, I can’t recall the author, but I do remember they hailed from the town of Zenthia.

In log.R, write R code to find the title of the book that The Teacher is referencing. Once you’ve found the title, write it in answers.txt.

Hint

The logical operator %in% returns whether a given value is in a vector—TRUE or FALSE. For instance, consider the following:

"Kenji Sato" %in% c("Jack Parker", "Kenji Sato")

which would return:

TRUE

Similar to other logical operators such as & and |, %in% is vectorized. Consider the following:

c("Alaric Runeweaver", "Kenji Sato", "Thalia Starbinder") %in% c("Jack Parker", "Kenji Sato")

which would return a vector of length three:

FALSE TRUE FALSE

Usage

Assuming log.R is in your working directory, you might find it most helpful to use Command + Enter (if on Mac) or Control + Enter (if on Windows) to run individual lines of code from log.R.

How to Test

check50

You can check your code using check50, a program that CS50 will use to test your code when you submit. But be sure to test it yourself as well!

Run the following command in the RStudio console:

check50("cs50/problems/2024/r/librarian")

Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.

How to Submit

You can submit your code using submit50.

Keeping in mind the course’s policy on academic honesty, run the following command in the RStudio console:

submit50("cs50/problems/2024/r/librarian")
Librarian - CS50's Introduction to Programming with R (2024)

FAQs

How much is CS50's Introduction to Computer Science? ›

Ways to take this course

A Verified Certificate costs $219 and provides unlimited access to full course materials, activities, tests, and forums.

What is CS50 r? ›

CS50 R is an introduction to programming using a language called R, a popular language for statistical computing and graphics in data science and other domains. Learn to use RStudio, a popular integrated development environment (IDE).

Does Harvard CS50 teach coding? ›

A gentle introduction to programming that prepares you for subsequent courses in coding.

Is CS50 Introduction to Computer Science? ›

An introduction to the intellectual enterprises of computer science and the art of programming.

Is CS50 too hard for beginner? ›

CS50 is pretty fast paced and honestly does not like the easiest intro class. You definitely have to put a lot of work into it to understand everything and ask a lot of questions. This course would've been a lot harder if I didn't have prior experience.

How long does it take to finish a CS50? ›

CS50: The Expanded Offering
LevelCoursesWorkload
Basic (Optional)CS50 Tech4 hours / 6 weeks
CS50 Scratch6 hours / 3 weeks
Core (Pick one: usually enough)CS5012 hours / 12 weeks
CS50 Law4 hours / 10 weeks
10 more rows
Apr 23, 2024

Why is CS50 so popular? ›

CS50 is a comprehensive and highly respected course that provides a strong foundation in computer science. As a self-taught frontend developer with no computer science degree, I found that the course was an excellent way to fill in knowledge gaps and strengthen my understanding of the fundamentals of the field.

Does CS50 give a free certificate? ›

Most notably, the course is entirely free, and it includes a free certificate of completion. But understanding how to obtain it can be a bit confusing. So in this article, let's discuss CS50 and explain how you can earn a free certificate.

What does CS50 stand for? ›

CS50 (Computer Science 50) is an on-campus and online introductory course on computer science taught at Harvard University and Yale University.

How many hours is CS50's introduction to computer science? ›

On average, the course takes between 10 to 20 hours per week to complete. A minimum of 9 assignments is needed to pass the course.

Is CS50 a first year course? ›

While students should be mindful of CS50's workload and should perhaps avoid taking 4 pset-based classes, students shouldn't shy away (from CS50 or any other introductory course) simply because they're first years.

Is CS50's Introduction to computer science certificate free? ›

Most notably, the course is entirely free, and it includes a free certificate of completion. But understanding how to obtain it can be a bit confusing. So in this article, let's discuss CS50 and explain how you can earn a free certificate.

How much does the CS50 certificate cost? ›

A Verified Certificate costs $299 and provides unlimited access to full course materials, activities, tests, and forums. At the end of the course, learners who earn a passing grade can receive a certificate.

Is CS50 computer science worth it? ›

Yes. In fact, around two thirds of students who enroll in CS50 have never taken a computer science course before. So if you're new to the field and looking to learn the basics, you'll be in good company.

References

Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 6214

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.