1. Basic Math (cont.1)
## Precedence
1+2+3/3
## [1] 4
(1+2+3)/3
## [1] 2
2. Variables
m <- 1
n <- "Hello World"
MyVector <- c(1,2,3,4)
AnotherVector <- c("a","b","c")
3. Numbers
4. Integers
x <- 5
y <- -2
z <- 5.8 #Note 5.8 is clearly not an integer
is.integer(y)
## [1] FALSE
k <- as.integer(z)
is.integer(z)
## [1] FALSE
5. Numerics
j = 1.5
k = 23.456
str(k)
## num 23.5
6. Vectors
# we want to store lists of numbers all in one place.
# One way to do this is using vectors in R. Vectors store several numbers.
a <- c(1,2,3,4,5)
b <- c(1234,2345,3456,4567)
6.1 Indexing an element in a vector
b[1]
## [1] 1234
b[3]
## [1] 3456
6.2 Indexing a range of elements in a vector
b[2:4]
## [1] 2345 3456 4567
6.3 Replacing an element in a vector
h <- c("You are so cute", 36, 99, 105)
h[1] <- "You are so smart"
6.4 Adding an element to the vector
h <- c("You are so cute", 36, 99, 105)
h_new1 <- c(h, "You are also smart")
h_new2 <- c("You are also smart", h)
h_new3 <- c(h[1:1], "You are also smart", h[2:4]) #insert a new element at position 2 of the vector
6.5 Checking the number of elements in your vector
length(h)
## [1] 4