In this sesssion we’ll be covering:
Other sessions we’ll cover today and tomorrow:
R is many things…
It’s not necessary to open RStudio to use R, but in these sessions we will assume that RStudio is your interface to R
When you first open RStudio, this is what you see:
1 + 1 then hit “Enter” and R will return the answer10 + 5
10 - 5
10 * 5
10 / 5
10 ^ 5
| Operator | Meaning | Example |
|---|---|---|
+ |
addition | 2 + 2 |
- |
subtraction | 2 - 2 |
* |
multiplication | 2 * 2 |
/ |
division | 2 / 2 |
^ or ** |
exponentiation | 2 ^ 2 |
R follows the usual order of arithmetical operations and uses parentheses for grouping operations.
10 - 3 / 5 # R will first do division operation then subtraction
## [1] 9.4
(10 - 3) / 5 # Use parentheses to group and prioritize operations.
## [1] 1.4
# in front of your comment# will not be evaluated# Full line comment
x # partial line comment
<-. You can use = or -> as well, but this is much less common and you will typically see the left arrow used.x and y by assigning some numbers to themx <- 10
y <- 5
x + y
## [1] 15
x
## [1] 10
x <- 20
x
## [1] 20
x has changedx <- 5
X <- 5
R has three main object types:
| Type | Description | Examples |
|---|---|---|
character |
letters and words | "z", "red", "H2O" |
numeric |
numbers | 1, 3.14, log(10) |
logical |
binary | TRUE, FALSE |
There are several ways to group data to make them easier to work with:
c( ) as a container for vector elements. Think of the c as combining elements.x <- c(1, 2, 3, 4, 5)
x
## [1] 1 2 3 4 5
fruit <- c("apples","bananas","oranges")
fruit
## [1] "apples" "bananas" "oranges"
If you try to type in text without using quotations marks (either single or double), R will throw an error. Try the text below.
fruit <- c(apples, bananas, oranges)
fruit
This is because R will interpret text without quotes as the names of variables. Since we don’t have any variables named apples, bananas, or oranges, R can’t find them.
list() as a container for list itemsx <- list("Benzene", 1.3, TRUE)
x
## [[1]]
## [1] "Benzene"
##
## [[2]]
## [1] 1.3
##
## [[3]]
## [1] TRUE
data.frame() as a container for many vectors of the same lengthpollutant <- c("Benzene", "Toluene", "Xylenes")
concentration <- c(1.3, 5.5, 6.0)
carcinogen <- c(TRUE, FALSE, FALSE)
my.data <- data.frame(pollutant, concentration, carcinogen)
my.data
## pollutant concentration carcinogen
## 1 Benzene 1.3 TRUE
## 2 Toluene 5.5 FALSE
## 3 Xylenes 6.0 FALSE
If you try to input a data frame where the elements are not all the same length, this will cause an error
pollutant <- c("Benzene", "Toluene")
concentration <- c(1.3, 5.5, 6.0)
carcinogen <- c(TRUE, FALSE, FALSE)
my.data <- data.frame(pollutant, concentration, carcinogen)
## Error in data.frame(pollutant, concentration, carcinogen): arguments imply differing number of rows: 2, 3
Now let’s try some exercises to test our understanding of R basics and data types.