1. Using a for or while loop, write a function to calculate the number of zeroes in a numeric vector.
nZero <- rep(0:9, each=4)
z <- vector(mode="numeric")

for(i in seq_along(nZero)){
    if(nZero[i] == 0) z[i] <- 1 else
    z[i] <- 0 
}
sum(z)
## [1] 4
  1. Use subsetting instead of a loop to rewrite the function as a single line of code.
length(nZero[nZero == 0])
## [1] 4
  1. Write a function that takes as input a numeric vector, and returns as output the maximum difference between all possible pairs of elements. Be careful to ensure that your function works properly with both negative and positive numbers. For your first version of the function, create a vector that stores all possible pairwise differences and then extracts the maximum value from that list.
# Alex's example (thanks for sharing your code, Alex!)
x= c(-6, 7, 8, -2, 4, 5)
#x = rnorm(20, mean = 0, sd =10)
max_diff = function(x=c(-6, 7, 8, -2, 4, 5)){
  #pairs = expand.grid(x,x)
  #diffs2 = abs(pairs[,1]-pairs[,2])
  
  #max_difference2(
    
  cols = rep(0,length(x)^2)
  x_pairs = data.frame(cols,cols)
  counter = 0
  for(i in 1:length(x)){
    for(j in 1:length(x)){
      counter = counter + 1
      iter_pairs<- c(x[i],x[j])
      x_pairs[counter,] = iter_pairs
}
  }
  diff = abs(x_pairs[,1] - x_pairs[,2])
  #max_diff = max(diff, na.rm = T)
  return(max(diff))
}
max_diff()
## [1] 14
  1. Now modify the output of (3) to yield a list with 3 elements. The first list item is the pair of vector values that are the maximum distance apart, the second list item is the pair of numbers representing the position of these elements in the vector, and the third list item is the maximum distance calculated from this pair.
x= c(-6, 7, 8, -2, 4, 5)

max_diff = function(x=c(-6, 7, 8, -2, 4, 5)){
  
  pairs = as.matrix(expand.grid(x,x))
  diffs = abs(pairs[,1] - pairs[,2])
  
  vec_max = which.max(diffs)
  
  pair_max = pairs[vec_max,]
  
  max_difference = max(diffs, na.rm = T)
  
  var1_vec = which(pair_max[1] == x)
  var2_vec = which(pair_max[2] == x)
  pair_vec = c(var1_vec, var2_vec)
  
  output = list(pair_max, pair_vec, max_difference)
  
  return(output)
  
}
max_diff()
## [[1]]
## Var1 Var2 
##    8   -6 
## 
## [[2]]
## [1] 3 1
## 
## [[3]]
## [1] 14
  1. For a second version of (3), store only a temp variable that keeps track of each difference and then retains only the largest difference as it cycles through the pairwise differences.
x= c(-6, 7, 8, -2, 4, 5)

max_diff = function(x=c(-6, 7, 8, -2, 4, 5)){
  
  pairs = expand.grid(x,x)
  diffs = abs(pairs$Var1 - pairs$Var2)
  temp_max = 0
  for(v in 1:length(diffs)){
    
    if(temp_max <= diffs[v]){
      temp_max = diffs[v]
    }
  }
  return(temp_max)
}
max_diff(x)
## [1] 14
  1. Write a function that takes as input two matrices, and then multiplies them together, using the rules of matrix multiplication. Your function should return a warning if the input matrices are not of the correct dimensions for matrix multiplication. Check the performance of your function by comparing with the built in R function for matrix multiplication %*%.
m1 = matrix(c(1:4),2,2, byrow = T)
m2 = matrix(c(5:8),2,2, byrow = T)

matrix_multpl = function(m1, m2){
  # create a new matrix table
  m3 = matrix(0, nrow(m1), ncol(m2))
  if (nrow(m1) != ncol(m2)){
    cat("input matrices are not correct dimensions", "\n")
  } else {
    for (nr in 1:nrow(m1)){
      for (nc in 1:ncol(m2)){
        cell_val = sum(m1[nr,]*m2[,nc])
        m3[nr,nc] = cell_val
      }
    }
  }
  return(m3)
}

matrix_multpl(m1, m2)
##      [,1] [,2]
## [1,]   19   22
## [2,]   43   50
m1%*%m2
##      [,1] [,2]
## [1,]   19   22
## [2,]   43   50
  1. Write a function that takes as input two integers representing the number of rows and columns in a matrix. The output is a matrix of these dimensions in which each element is the product of the row number x the column number.
m1 = matrix(c(1:10),2,5, byrow = T)

matrix_prod = function(nr, nc){
  m = matrix(0, nr, nc)
  for (r in 1:nr){
    for (c in 1:nc){
      m[r,c] = r*c
    } 
  }
  return(m)
}
matrix_prod(4, 6)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    2    3    4    5    6
## [2,]    2    4    6    8   10   12
## [3,]    3    6    9   12   15   18
## [4,]    4    8   12   16   20   24