Function List

Functions in this document will be from the 9/24/20 lecture

For loops in R

Need to make a variable i.e. i

i in ___ so in the below i takes the integer between 1 and 100

Below, making a for loop, range 1:100, print i and increment

Never use i within the for loop, it may cause errors

eval = F allows us to run code but when we knit it i doesnt run, just print the code

for(i in 1:100) {
  print(i)
}

is.na any.na

is.na will tell you exactly which are false and which are true

any.na will tell you simply if there are any NA

x <- c(10,20,40,50, NA)
is.na(x)
## [1] FALSE FALSE FALSE FALSE  TRUE
#if any are NA, it will return True
any(is.na(x) == FALSE)
## [1] TRUE

Vectorized Notation

Calculate lambda

length() gives length of a vector

Staggering and dividing allows for faster calculation all at once

wolves <- c(10, 11, 16, 13, 14)
wolves[1:length(wolves)-1] / wolves[2: length(wolves)]
## [1] 0.9090909 0.6875000 1.2307692 0.9285714

Draw things randomly out of a hat

Make a vector containing a series of nummbers in this case 1:6 and randomly pick them out

Sample function allos you to pick the vector, number of items to pick out, and weather or not to put them back

d6 <- 1:6
sample(x = d6, size = 1, replace = TRUE)
## [1] 5

Natural log

log(5)
## [1] 1.609438

Plotting

Use plot function and tilda as function of two datasets

wolves <- c(10, 12, 15, 16, 17, 14, 16, 18, 12, 11)
deer <-   c(100,120,150,160,170,140,160,180,120,110)
year <- c(2000:2009)

plot(wolves~deer, type = "b")

Note

When assigning names, make sure not to use functions pre written in r

Options for making lists

d <- c(3,4,5,6,7,8,9,10)
d <- 3:10
d <- c(3:10)
d <- seq(3,10)
d <- seq(3,10,1)
d <- seq(from = 3, to = 10)
d <- seq(from = 3, to = 10, by = 1)