The goal of our first meeting with R is to:
… with a vocabulary and syntax:
… with dialects
You don’t need to memorize words, and you can say things in many different ways. Soon you will find your own style!
versatile and updated, but messy
facilitated for documentable analyses
We start by clicking on the correct icon. We will open RStudio, not R.
Now we have three windows open: R console, the environment, and the window where we see our figures. We will initially ignore the last two windows.
We start with the “console”. This is a window for direct communication to R. In the future, you will only use this for minor things.
Now we can begin the dialog!
Imitation Game It takes place by me stating what I’m going to say in “quotation marks”. R repeats after me but performs no operations itself.
"Hi!"
## [1] "Hi!"
Dialog - calculator I can use R as a calculator. Then I ask, and R answers.
2+2
## [1] 4
R is familiar with most mathematical operations and practically resembles your pocket calculator (if you still remember it).
Don’t smile, don’t laugh We can ask R to ignore what
we say with #
.
# ignore this
It is useful when we document our codes.
We have a better workflow when we use an R script. We open a new
script through the “File” menu or by pressing “ctrl+shift+N”. We save
through the file menu with the extension “.R”: script.R
We write on the notepad before we “send” the code down to R. We can do this by * highlighting the text and pressing “Run” (top right) * “ctrl+Enter”
Now we can continue our dialog.
We can save information in objects. These are digital storage boxes. If we don’t save information in objects, it is lost!
Here is the object two
.
two <- 2
Synonyms We put information in the storage box
(object) with an arrow into the object (<-
) or with an
equals sign (=
).
two = 2; two <- 2
When we save information in an object, R does not give the answer in the console but places it directly in the object.
We can check what is in the object.
two
## [1] 2
Perform calculation operations on the object
two+2
## [1] 4
Overwrite the object (NB: you always overwrite; there is nothing called “updating” an object).
two <- 3
two
## [1] 3
Create a new object.
three <- 3
Perform operations on two objects.
two+three
## [1] 6
Save in a third object.
five <- two+three
R is an object-oriented language. We save all information in objects located in our workspace (in short-term memory when RStudio is open).
Examples of objects:
Is it true? We can ask R if something is true
five == 5
## [1] FALSE
Negation Is the object not equal to 5?
five != 5
## [1] TRUE
Sizes Is five
more than 6?
five > 6
## [1] FALSE
Equal to or more than?
five >= 6
## [1] TRUE
Multiple conditions Is five less than 6 and more than 0?
five < 6 & five > 0
## [1] FALSE
Is five more than 6 or less than 0?
five >= 6 | five > 0
## [1] TRUE
When you use “or,” it is wise to pack things in parentheses. Then you can combine both “and” and “or” in the same condition. It identifies several groups/subgroups.
This is golden when we check and recode data.
Conditional recoding Let’s correct our object by giving R a condition.
if(five != 5){
five <- 5
}
#Check what happened
five
## [1] 5
Use the table below and play around!