This presentation has been written and aimed at new R users. We introduce rudimentary R functionality and data types.
vect <- c(1, 2, 3, 4, 5, NA)
vect1 <- 1:5
vect2 <- seq(from = 1L, to = 10L, by = 2)
class(vect) # calling a function on vect
## [1] "numeric"
print(vect)
## [1] 1 2 3 4 5 NA
vect1
## [1] 1 2 3 4 5
vect2
## [1] 1 3 5 7 9
# Sum the vectors
vect1 + vect2
## [1] 2 5 8 11 14
# Sum the 3rd and 5th values in vect1 and vect2 respectively
vect1[3] + vect2[5]
## [1] 12
# Average of vect <- c(1, 2, 3, 4, 5, NA)
mean(vect)
## [1] NA
# First remove the NA value, then compute the mean
mean(vect, na.rm = TRUE)
## [1] 3
# matrix
mat <- matrix(sample(1:5), nrow = 3, ncol = 5, byrow = FALSE)
mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 5 3 2 1 4
## [2,] 2 1 4 5 3
## [3,] 4 5 3 2 1
colnames(mat) <- c("one", "two", "three", "four", "five")
mat
## one two three four five
## [1,] 5 3 2 1 4
## [2,] 2 1 4 5 3
## [3,] 4 5 3 2 1
# Exercise: Print the entry in row 2, column 4
p <- data.frame(vect1, rep(NA, 5), vect2)
p
## vect1 rep.NA..5. vect2
## 1 1 NA 1
## 2 2 NA 3
## 3 3 NA 5
## 4 4 NA 7
## 5 5 NA 9
colnames(p)[2] <- "Missing"
head(p, 3)
## vect1 Missing vect2
## 1 1 NA 1
## 2 2 NA 3
## 3 3 NA 5