remove a variable x
rm(x)
special constant for:
missing or undefined data
NA
special constant for:
empty object
NULL
special constant for:
positive / negative infinity?
Inf / -Inf
special constant for:
results that cannot be reasonably defined
NaN
(not a number)
Check if a number eg x is finite?
is.finite(x)
How to combine Values into a Vector or List?
sio
c: Combine Values into a Vector or List
eg
c(1,7:9)
c(1:5, 10.5, “next”)
what are the three common tasks that c() can be used for?
What does c stand for?
c stands for combine
Check if something:
Make a vector that holds true, true, false
v4 <- c(v1,v2,v3,”boo”)
What type will the elements be?
all strings
Make a vector with the numbers from 1 to 7
v <- 1:7 # same as c(1,2,3,4,5,6,7)
Make a vector with 0 repeated 77 times
v <- rep(0, 77) # repeat zero 77 times: v is a vector of 77 zeroes
Make a vector with the even numbers from 10 to 20
v <- seq(10,20,2) # sequence: numbers between 10 and 20, in jumps of 2
Make a vector with 1,2,3,1,2,3
v <- rep(1:3, times=2) # Repeat 1,2,3 twice
Make a vector with the numbers 1,1,2,2, … up to 10
v <- rep(1:10, each=2) # Repeat each element twice
check the length of vector v?
length(v)
How can you add the elements of two vectors together?
eg
v1 <- 1:5 # 1,2,3,4,5
v2 <- rep(1,5) # 1,1,1,1,1
element wise addition:
v1 + v2
How can you
Mathematical operations
Consider a vector v. How can you get the
Logical operations - consider vectors v1 and v2:
compare each element in v1 to corresponding elements in v2 / to the number 2, and return a logical vector
v1 < v2 / v1 < 2
v1 > v2
v1 >= v2
…
v1 == v2
v1 != v2
Logical operations - consider vectors v1 and v2:
Generate a logical vector which checks if each element in v1 is greater than 2 OR the corresponding element in v2 is greater than 0
(v1>2) | (v2>0) # | is the boolean OR, returns a vector (brackets optional)
If either of the statements are true –> TRUE
if neither is true –> FALSE
Logical operations - consider vectors v1 and v2:
Generate a logical vector which checks if each element in v1 is greater than 2 AND the corresponding element in v2 is greater than 0
v1>2 & v2>0
(v1>2) & (v2>0)
How do you access the ith element of a vector v?
v[i]