Lab: R Basics

Why are we here?

Now that we have our R and RStudio computing environment set up, let’s start working in base R. Base R is the pre-programmed R software, in other words, the functionality available without using any R packages like tidyverse.

This lab covers:

Optional: supplemental resources for review and practice

Lab Goals

The purpose of this lab is to introduce you to working with base R.

After completing this lab, you should understand

  • Creating objects

  • Prebuilt functions

  • Commenting

  • Data types

  • Using the accessor $, [ ], or [[ ]]

  • Vectors

  • Sequences and subsetting

Lab instructions

Setting up

  • In RStudio, create a new R Script called lab3_yourname.R

    • Save in a new folder within your CMSC 121 project working directory:
      • IMPORTANT: If you have not already, create a new folder in your working directory titled called labs, place your .R file in the new folder.
  • Place your answers to the p in the .R file

Complete and Submit on Brightspace

Exercises to submit in your .R script file.

IDS Chapter 2 Exercises 1-18, and 20-23


For Exercise 1 – put the answer you get into a comment in the file.

For Exercise 2 – copy in and edit the Exercise 1 code so that it will generate the Exercise 2.

Exercise 3 - put the answer in a comment in the file

Exercise 4 - put your one line of code in the .R file

Exercise 5 - you can try these out in the Console, then put your answer in a comment in the .R file

Exercise 6 – show your str() command and put the answer in a comment

Exercise 7 – use code to generate your answer, and put your answer in a comment

Exercise 8 – put the $ instruction you used in the .R file, also put in the instruction you used to determine the class of the object

Exercise 9 – put your code in the .R file

Exercise 10 – put your code in the .R file

Exercise 11 – put your code in the .R file

Exercise 12 – rewording: Use the c() function to create the vector temp with the average high temperatures in January for Beijing, Lagos, Paris, Rio de Janeiro, San Juan, and Toronto, which are 35, 88, 42, 84, 81, and 30 degrees Fahrenheit. – put your code in the .R file

Exercise 13 – rewording: Now create the vector city with the city names. – put your code in the .R file

Exercise 14 – put your code in the .R file

Exercise 15 – put your code in the .R file

Exercise 16 – put your code in the .R file

Exercise 17 – put your code in the .R file

Exercise 18 – put your code in the .R file

Exercise 20 – put your code in the .R file (note: skip Exercise 19)

Exercise 21 – put your code in the .R file

Exercise 22 – put your code in the .R file

Exercise 23 – put your code in the .R file