Four panes
Small tips
Learn how to
RStudio Tip
You can run the selected codes by hitting
command
+ enter
Control
+ enter
Contents enclosed by double (or single) quotation marks will be recognized as characters.
You cannot do addition using characters
We will learn string manipulations later using the stringr
package.
<-
or =
.<-
or =
to use. You should pick whichever makes sense for you (though it is often recommended to use <-
). But, it is a good idea to be consistent.Notice that these objects are now in the list of objects on the environment tab of RStudio.
Note
You can insert the assignment operator (<-
) by hitting
Option
+ -
Alt
+ -
Once objects are created, you can evaluate them on the console to see what is inside:
Note
I often ask you to evaluate an R object. That just means looking inside the R object to see what is inside.
Several things to remember about assignment:
“Everything is an object and everything has a name.”
R has many different object types (classes)
Definition
A vectors is a class of object that consists of elements of the same kind (it can have only one type of elements). You use c()
to create a vector.
Example
Different modes?
What if we mix elements of different mode
All the numeric values are converted to characters.
Definition
A list
is a class of object that consists of elements of mixed types.
Example
list
is very flexible. It can hold basically any type of R objects as its elements.Definition
A matrix
is a class of object that consists of elements of the same kind (it can have only one element) stored in a two-dimensional array.
Examples
data.frame
is like a matrix (or a list of columns)
There are different kinds of objects that are like “data.frame”
We will talk about some of them later.
It is critical to recognize the class of the objects:
Many of the errors you will encounter while working on R has something to do with applying functions that are not applicable to the objects you are working on!
Use class
, typeof
, and str
commands to know more about what kind of objects you are dealing with:
A function takes R objects (vector, data.frame, etc), processes them, and returns R objects
Example:
min()
takes a vector of values as an argument and returns the minimum of all the values in the vector
Functions (both base and user-written) are what makes R compelling to use as major statistical and programming software!
Indeed, this course is pretty much all about learning useful functions that make your life easier
We will learn lots of functions that are made available through user-written packages
\(\frac{1}{n}\sum_{i=1}^n x_i\)
\(\frac{1}{n}\sum_{i=1}^n (x_i-\bar{x})^2\), where \(\bar{x}\) is the sample mean
A drawer in your work space (R environment) that has specialized tools (functions) to complete tasks.
Example packages:
library()
function:You are the architect who has the blueprint of the final product, but does not have an ability to build specific pieces by yourself
You work with one worker (R) who can build specific pieces perfectly without any error if given right tools and instructions
This worker is weird. If you do not give right tools or your instruction is wrong, he/she will speak up and tell you there has been an error. He/she will not try to figure out how things could have been done differently by himself/herself.
Your job is to provide the right tools and instructions to the worker (R), and correct your instructions when you found out you made a mistake (debugging)
Let’s define two vectors to work with
Note
Vector arithmetic operations happen element by element!
To access element(s) of a vector, you use []
like below:
You can access multiple elements of a vector
To access element(s) of a matrix, you can use either [[]]
operator for accessing a single element and use []
for multiple elements.
Example: single element
Example: multiple elements
You can also use $
operator to access a single element of a list as long as the element has a name.
data.frame
(and its relatives)
list
of vectors that are of the same length that makes a matrix
-like structureAccessing parts of a data.frame
works like accessing elements of a matrix or list.
We will spend lots of time on how to do data wrangling on data.frame
s using the tidyverse
package!