Learning Objectives

Technical sills and concepts:

  • arithmetic operators
  • script and console
  • assign a value to a variable
  • create a code comment
  • identify the data type of an R object
  • Subsetting by position and name.

Big-picture concepts

  • Begin to develop comfort level working with a computer language.
  • Understand the advantage of using variables instead of raw values.
  • What is abstraction?
  • What is a data type?

Introduction

This assignment covers some basic, yet very important concepts. Believe it or not, all of the more complicated and sophisticated computing techniques in this course and beyond are built upon a small set of operations like the ones we are learning.

Try to keep in mind the big-picture concepts listed above as you work through the technical exercises. They will help you understand and appreciate how the technical skills (which will get more technical) relate to the larger picture!

Key Terms

During this and the following several labs, you’ll become familiar with all of these terms. It will be essential for you to know what each of them means as you continue to build your R and computing skill set:

  • Operating system

  • Application

  • Graphical user interface (GUI)

  • Command line

  • Hard drive and memory (and what is the difference?)

  • Filesystem concepts

  • Files and directories

  • Subdirectory/subfolder

  • Path

  • Absolute and relative paths

  • Root directory

  • File concepts

  • File name

  • Text file

  • Zip/archive file

  • File extension and association

Strings and Expressions

R considers any text contained within quotes to be a string of literal text. It won’t attempt to interpret its meaning.

R will attempt to interpret any non-quoted text you type into the R Console (or a script file) as an expression to be evaluated.

Instructions

DataCamp course

Work through the chapters of the Introduction to R course in DataCamp.

The lecture course will have a set of basic questions regarding the concepts in the Intro to R course, the lab will build on these with more technical questions.

I suggest you complete the lecture assignment before beginning this lab assignment.

Course RProject

To keep our lab files organized, we’ll store them in a subdirectory of our main course folder.

  • Check out the instructions in the lecture software setup assignment for directions on how to create an RProject.
  • You should complete the RProject section of the Software Setup assignment before proceeding.


From now on, you’ll use always the course RProject to do your work in RStudio. When you start up RStudio, always check the upper right corner of the RStudio window to verify that you have the correct RProject

Lab Directory Setup

Finding and loading files into R is one of the greatest initial hurdles for new R users.

To that end everybody will set up a common folder structure for the course so that it is easier to collaborate with classmates and to ask questions.

After you’ve created your course directory structure and created your course RProject (instructions are in the software setup lecture assignment) create a labs subdirectory of your main course folder. Within the labs folder, create a subdirectory called lab_01.

You’ll create a new subdirectory of labs for each lab assignment.

Lab 1 Script

To do your work on labs 1 and 2, you should create R script files. R scripts are text files that contain instructions to be sent to the R console.

  • Using the File menu, select New File and choose the R Script option.
    • Your file will initially be called “untitled1” or something similar.
  • Save your script file in your lab_01 directory. I suggest giving it a descriptive filename like lab_01.R.
    • Note that R script files have the file extension .R.

Lab Questions

Expressions and Strings: Question 1

Run the following two lines of code in the console.

c(1, 2, 3)
"c(1, 2, 3)"
  • Q1 (2 pts.): Explain why the outputs of the two lines are different.

Variables: Questions 2 - 4

Run the following two lines of code in the console and consider the differences:

c_1 = c(1, 2, 3)
c_2 = "c(1, 2, 3)"
  • Q2 (1 pt.): Is c_1 a variable, or a function? How do you know?
  • Q3 (1 pt.): Is c_2 a variable, or a function? How do you know?
  • Q4 (1 pt.): If c_1 and c_2 have different values, why?

Matrices 1: Questions 5, 6

Create a numeric vector of length 6 called my_vec. It should contain the integers from 1 to 6. Build a matrix using the following code:

mat_1 = matrix(my_vec, nrow = 3)
  • Q5 (1 pt.): What are the dimensions of the matrix (i.e. how many rows and columns)?

  • Q6 (2 pts.): Write R code to retrieve the element of mat_1 that has a value of 3.

Matrices 2: Questions 7 - 11

You will use my_vec from the previous question again.

Create a matrix mat_2 that has two rows and three columns using my_vec. Do not use the c() or rep() functions.

Create a matrix mat_3 that has three rows and two columns using my_vec. Do not use the c() or rep() functions.

  • Q7 (1 pt.): Paste the code you used to create mat_2.
  • Q8 (1 pt.): Paste the code you used to create mat_3.
  • Q9 (1 pt.): Did R use rows or columns to recycle/distribute the values in my_vec?
  • Q10 (1 pt.): Using my_vec, create a matrix, mat_4. mat_4 must have a total number of elements that is not a multiple of 6.
  • Q11 (1 pt.): How did R handle the recycling/distributing of values of my_vec in mat_4?

List Subsetting: Questions 12 - 14

Subsetting objects is a key skill when working with R.

Typical questions we might ask:

  • By position. What is the value of the third element in the object?
  • By name. What is the value of the element called “abc” in the object?
  • Logical subsetting. Which elements of the matrix are equal to 3?

Create a list, named my_list_1 with following three elements:

  • first element is numeric: 5.2
  • second element is a string “five point two”
  • third element is a vector of all integers from 0 to 5.

Do you recall how to do this from the DataCamp course?

Name the elements in my_list_1:

  • “two”
  • “one”
  • “three”

NOTE: Yes they are out of order!!!

Run the following 8 lines of code.

my_list_1[[1]]
my_list_1[[as.numeric("1")]]
my_list_1[["1"]]
my_list_1[["one"]]
my_list_1$one
my_list_1$"one"
my_list_1$1
my_list_1$"1"
  • Q12 (8 pts.): For each of the 8 lines, answer the following: A. Did the line return a 1: value, 2: error, or 3: NULL? B. What type of subsetting operation was used (or attempted)? C. If it did not return an error describe, in ordinary English, a plausible explanation of how R could have performed the subsetting.

  • Q13 (2 pts.): Identify which lines produced the string output "five point two" and explain why.

  • Q14 (1 pt.): Identify which lines produced NULL output and explain why.

Report

Compile your answers to all 14 questions into a pdf document and submit via Moodle.