Lessons 0 to 3
Lesson 0 - Install R and RStudio
Install R on your computer. Pick the one that fits your system (PC vs Mac; what version of Windows vs what version of Mac OS). For PC's, check if your PC is 64-bit or 32-bit before downloading R and RStudio.
Choose the CRAN location closest to you.
Then install RStudio. Again download the one that matches your computer.
https://www.rstudio.com/products/RStudio/
Let me know when this is done or if you need help.
Watch the above video about navigating RStudio [20 min]
Lesson 1 - Basic Syntax in R
A. Types of R Object Classes
Numeric -- this is a number, such as 1, 22.3, 450, 3e23
Logical -- only two options here: TRUE or FALSE. Typing in 2==3 will return the word FALSE on your screen. Typing in 2=2 will return the word TRUE.
Character -- a string of letters on which you cannot do math. Example: "Joe", "This is a sentence". Strings need to have quotation marks around them.
Expression -- this is a command that R can execute to accomplish a task. It looks like a characters string but is not. 4*3 is a command that says multiply 4 with 3. "4*3" is a character string because the quotation marks make R think that 4*3 is a sentence.
B. How to check the class of an object:
Use the class() function!
Given a = c(1,4,10), then typing class(a) will return "vector"
Given b = list("joe", 23, "dog"), then typing class(b) will return "list"
C. How to Create Objects in R to Represent Information
Note 1: R allows you to store lots of information in the form of "objects" , which are just symbols. For example: I can store a phone number (123) 456-7890 in an object called "call.this". After I've created the object called "call.this", every time R sees this object in my code, what it really thinks is "(123) 456-7890".
Note 2: object names must start with a lowercase letter.
--
Method 1 for creating assigning meaning to objects:
Use the "<-" symbols, which means "assign". Looks like a left arrow.
a <- 23 means "use the letter a to store the number 23"
my.friend <- 23 means "use my.friend to store the number 23"
If you type in "a" or "my.friend" after creating these objects, then R will spit out the number 23 on your screen.
--
Method 2 for creating assigning meaning to objects:
Use the "=" symbols, which also means "assign" just like "<-"
a = 23 means "use the letter a to store the number 23"
my.friend = 23 means "use my.friend to store the number 23"
If you type in "a" or "my.friend" after creating these objects, then R will spit out the number 23 on your screen.
D. Mathematical and Logical Operators in R
+ --- means plus, or add
- --- means subtract
* --- means multiply
/ --- means divide
() --- use parenthesis according to Please Excuse My Dear Aunt Sally
Logical Operators:
== --- means equal to
!= --- means NOT equal to
> --- means greater than
< --- means less than
>= --- means greater than or equal to
<= --- means less than or equal to
E. How to Create Objects that Contain More than One Item
1. c() -- c stands for concatenate, which means to combine. this built-in function combines whatever is in the parenthesis into a vector of objects.
2. list() -- this built-in function creates a list of objects. Unlike a vector, a list can contain numbers and strings.
3. matrix() -- this built-in function that creates a matrix of items (a matrix is a 2D table that has rows and columns)
length() -- this built-in function tells you how many items are in whatever is in the parenthesis. It returns a number.
Example:
Given: a = list("joe", "jane", "burger")
length(a) will return the number 3, because there are three items in the list called "a"
dim() -- this tells you the numbers of rows and columns in a 2D object
If a matrix called my.matrix has 2 rows and 5 columns, then dim(my.matrix) will return: 2 5, meaning 2 rows 5 columns
################################################
Quiz
[Include the questions when you send me the answers]
What type of object class are the following:
1. TRUE
2. Bob, Sally, 23
3. "Bob, Sally, 23"
4. 2 + 2
5. "2 + 2"
Explain what the following lines do:
1. a = 24
2. a <- 24
3. b = c("joe", "jane", "jack")
4. c = list("joe", "jane", "jack")
Write code to do the following:
1. Create a vector called v1. This vector should contain the names jack, tom, and linda.
2. You have a list that contains numbers of your favorite fast-food restaurant. How do you find out how many names are in your list?
3. Create an object out of the full name of your high school. Name this object my.hs
Lesson 2 - Lists
A list can contain other lists and even multidimensional objects (like 3x3 tables).
the.list = list(c(“Mon”, “Tue”, “Wed”), matrix(c(1,2,3,4,5,6), nrow = 2), list("gold",7))
—
How to name the elements in a list:
names(the.list) = c("days", "some_matrix", "metal")
Rename the 2nd object in the.list:
names(the.list)[2] = “the.matrix”
—
How to create a list that has names for each item
mylist = list(days = c(“Mon”, “Tue”, “Wed”), foods = c(“fries”, “soda”, “pizza”))
—
length(the.list)
—
How to extract a single item in a list
the.list[3] will return the third item in the list
the.list$metal
—
How to extract all items in the list except one of them
the.list[-3] will return the items except for the third one
—
How to delete an item from a list
the.list[3] = NULL
the.list$metal = NULL
—
how to merge multiple lists
list.a = list(1:5)
list.b = list(letters(1:5))
list.c = c(list.a, list.b)
—
How to add items to a pre-existing list
Use the append() function
v1 = list(1:5)
stuff = “word”
v2 = append(v1, stuff)
1 2 3 4 5 word
v3 = append(v1, stuff, after = 3)
1 2 3 word 4 5
============================
Quiz
[Please include the questions when you send me the answers.]
Write the code for the following actions.
1. Create a list that contains the first names of your immediate family members.
2. Create a list that contains the following lists of things: (1) your current course names, (2) your teacher names, (3) your current grade in each course, (4) whether each course has an AP test in May. Each item in your main list should have a name.
3. Working off your answer to question 2: change the name of the third item in your main list to be "Progress".
4. Working off your answer to question 2: Determine the length of your main list.
5. Working off your answer to question 2: Determine the length of your 2nd list within your main list, all in one line of code. Hint: $ or [ ]
Lesson 3 - Useful Built-in Functions in R-Base
=================
seq()
The sequence function creates sequences of consecutive numbers.
seq(1:10) will yield 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
seq(1, 10, by = 2) will give 1, 3, 5, 7, 9
The first argument ("1") is where to start. The second argument ("10") is where to end. The third argument ("by=2") is how much to skip.
seq(0, 10, by = 2) will give 0, 2, 4, 6, 8, 10
================
rep()
This is the repeat function.
rep("joe", times = 3) will yield "joe" "joe" "joe"
rep(150, times = 5) will yield 150 150 150 150 150
================
paste()
This function prints strings in quotation marks.
paste("hi there") yields "hi there"
paste("hi", "friend") yields "hi friend"
================
paste0()
paste0 is like paste, but does so with spacing between INDIVIDUAL strings.
paste0("hi friend") yields "hi friend"
paste0("hi", "friend") yields "hifriend". There is no space in between the individual strings "hi" and "friend".
================
letters[], LETTERS[] {not the square brackets! not parentheses}
letters[] prints alphabet letters in lowercase
LETTERS[] prints alphabet letters in uppercase
letters[1:5] yields "a" "b" "c" "d" "e"
LETTERS[1:3] yields "A" "B" "C"
================
combine rep(), seq(), and paste()
these three are great to printing repeating things that differ each time.
prefix = rep("dan_", times = 3) yields "dan_" "dan_" "dan_"
suffix = seq(1:3) yields 1 2 3
merge = paste(prefix, suffix) yields "dan_1" "dan_2" "dan_3"
first3 = letters[1:3] yields a b c
merge_abc = paste(prefix, first3) yields "dan_a" "dan_b" "dan_c"
================
eval(parse(text=i)
This function removes the quotations marks around a string and executes whatever is left as a command.
v1 = "1+1" is a string, not an expression
v1 yields "1+1", not 2
answer1 = eval(parse(text = v1) yields 2
v2 = "b=2+2"
answer2 = eval(parse(text = v2 ) yields 4
but if you call the object "b", R returns 4. When you eval-parse-texted v2, you created a variable called b!
=========================
Quiz
[Include the questions when you send me answers.]
1. Write code to repeat a series of numbers from 5 to 11
2. Write code to repeat numbers from 3 to 9 by increments of 3.
3. Write code to paste first names of all your immediate family members, but without spaces between them.
4. Use rep(), seq(), and paste() to create your name 5 times. Each time should have a different integer starting from 3.
5. Same as in question 4, but difference with each consecutive name's number should be that the number also has letters of the alphabet starting from d (i.e. 10i, 11j, 12k).
6. Write code to turn a string into a command using eval(parse(text= )). The command is to create a variable called k. This variable represents the result of four divided by 2.