Chapter 3 Lab 2: Basic R Programming and Vector Operations
Objectives:
- To understand what an object is in R
- To identify and create vectors
- To learn how to differentiate types (classes) of vectors
- To perform simple math operations on numeric vectors
This chapter focuses on getting comfortable with the R console, scripting, and packages, and on the core syntax you will use for the rest of the course: creating objects, and building and operating on vectors.
3.1 The R console and basic math
Open RStudio and locate the console pane. You can type any of the following directly into the console and hit Enter:
## [1] 4
## [1] 7
## [1] 42
## [1] 25
## [1] 32
R follows the standard order of operations (PEMDAS), and you can use parentheses to control it, just like a calculator.
Question 1
-
Use the R console to calculate the area of a circle with radius 4.5
(Hint:
piis a built-in constant in R) -
Calculate
(12+8)/4^2using R. Add the code and the result.
3.2 Objects
R can store any type of variable into an object. To create an object, write the name of the object, followed by <-, followed by what you want to store.
## [1] 5
You’ll see the new object x appear in your Environment pane (upper right of RStudio). That confirms your object has been created and is available for future use in this session.
3.3 Basic data structures in R
A basic data structure is an n-dimensional object that stores information. Data structures in R are classified by their number of dimensions:
- Uni-dimensional data structures such as vectors
- Two-dimensional data structures such as data frames or matrices
- N-dimensional data structures such as lists (not covered in this course)
## [1] 1 2 3 4 5
## [1] "A" "B" "C" "D" "E"
## c.1..2..3..4..5. c..A....B....C....D....E..
## 1 1 A
## 2 2 B
## 3 3 C
## 4 4 D
## 5 5 E
3.4 Unidimensional objects: Vectors
A vector is a one-dimensional object — think of it as a “list” of elements in only one direction, with no rows or columns.
## [1] 1 2 3 4 5
## [1] "A" "B" "C" "D"
To create a vector, use the c() function (short for “combine”), which combines the values you separate by commas.
R Functions
Functions are the commands R uses to interpret an order.
c() means “Combine Values into a Vector.”
Syntax: function(object)
To measure the length of a vector, use the function
length():
## [1] 5
Question 2
-
Create three vectors:
-
One named
wordswith your five favorite words -
One named
numberswith your lucky numbers -
One named
mixedwith three numbers and three letters, in any order
-
One named
- Show the code you used to create the vectors
3.4.1 Classes of vectors
Use the class() command to identify the type of vector you’re working with:
## [1] "numeric"
## [1] "character"
R shows either numeric/integer or character. Character vectors are surrounded by quotation marks ("); numeric vectors are not.
Question 3
-
What are the classes of your
words,numbersandmixedobjects from Question 2? -
What class does R assign to a vector that mixes numbers and letters,
like
mixed? Why do you think that happens?
3.5 Math operations on vectors
Because vectors can hold multiple numbers at once, math operations are applied element-wise:
## [1] 11 12 13 14 15
## [1] 2 4 6 8 10
## [1] 1 4 9 16 25
You can also do math between two vectors of the same length:
## [1] 11 22 33 44 55
## [1] 10 10 10 10 10
Useful summary functions for numeric vectors:
## [1] 15
## [1] 3
## [1] 5
## [1] 1
Question 4
-
Create a numeric vector called
measurementswith 6 numbers of your choice -
Calculate the sum, mean, maximum, and minimum of
measurements. Add the code and results -
Multiply every value in
measurementsby 2.54 (as if converting inches to centimeters). Add the code and the resulting vector
3.6 Expanding vectors
If you have two vectors of the same class, you can combine them using c() again:
## [1] "A" "B" "C" "D" "A" "B" "C" "D"
## [1] 1 2 3 4 5 1 2 3 4 5
## [1] 1 2 3 4 5 6 7 8 9 10
Question 5
- Increase each of your vectors from Question 2 by two or three more elements. Add the code and the results.
-
Create an object that combines your
wordsandnumbersvectors. Add the code. - What is the class of this combined object? Explain why, based on what you learned in Question 3.