Number 1

A function to calculate the number of zeroes in an atomic vector v

zeroSum <- function(v=rep(-10:10,times=3)){
w <- vector(mode="numeric")
for (i in seq_along(v)){
  if(v[i]==0) w[i] <- 1 else
   w[i] <- 0
}
return(print(sum(w)))
}

zeroSum()
## [1] 3

Number 2

Using subsetting rather than for loops to accomplish the same thing as in # 1

v=rep(-10:10,times=3)

length(v[v==0])
## [1] 3

Number 3

Code for the function maxDiff() which returns the maximum difference between all possible pairs of elements in an atomic vector

maxDiff <- function(x=rep(-5:5,each=1)) {
   diff <- c(dist(x))
  return(max(diff))
}

maxDiff()
## [1] 10

Number 4

Altering the maxDiff function to output the values and numbered positions of the maximally distant numbers in the atomic vector as the second and third elements in the output

Based on code shared by Alex Burnham

maxDiff1 <- function(x=rep(-5:5,each=1)){
  mat <- matrix(nrow=length(x),ncol=length(x))
  rownames(mat) <- x
  colnames(mat)<- x
    for (i in seq_along(x)){
    for (j in seq_along(x)){
     mat[i,j]<- x[i]-x[j]
    }
  }
  z <- c(mat)
  x <- which(mat==max(abs(mat)), arr.ind=TRUE)
  w<-colnames(mat)[x[1,1]]
  t<-rownames(mat)[x[1,2]]
  return(list(c(w,t),x,max(abs(z))))
  }
   
maxDiff1() 
## [[1]]
## [1] "5"  "-5"
## 
## [[2]]
##   row col
## 5  11   1
## 
## [[3]]
## [1] 10

Number 5

Making a function maxDiff2 which does the same thing as maxDiff1 but that works by storing a temporary variable that keeps track of differences and retains the largest

Based on code shared by Alex Looi

 maxDiff2 <- function(x=rep(-10:10,by=1)){
  tempPairs =expand.grid(x,x)
  diffPairs = abs(tempPairs$Var1 - tempPairs$Var2)
  temp_max = 0
  for(v in 1:length(diffPairs)){
    
    if(temp_max <= diffPairs[v]){
      temp_max = diffPairs[v]
    }
  }
  return(temp_max)
}

maxDiff2()
## [1] 20

Number 6

A function that takes as input two matrices and multiplies them together using the rules of matrix multiplication

Based on code shared by Alex Looi

matMult = function(m1=matrix(data=1:10,nrow=2,ncol=5),m2=t(m1)){
   m = matrix(0, nrow(m1), ncol(m2))
  if (nrow(m1) != ncol(m2)){
    cat("input matrices are not correct dimensions", "\n")
  } else {
    for (i in 1:nrow(m1)){
      for (j in 1:ncol(m2)){
        cell_val = sum(m1[i,]*m2[,j])
        m[i,j] = cell_val
      }
    }
  }
  return(m)
}

# test function with default parameter
matMult()
##      [,1] [,2]
## [1,]  165  190
## [2,]  190  220
# compare results to those obtained by using the built-in R function
m1=matrix(data=1:10,nrow=2,ncol=5)
m2=t(m1)
m1%*%m2
##      [,1] [,2]
## [1,]  165  190
## [2,]  190  220

Number 7

A function that takes two integers representing the numbers of rows and columns in a matrix and returns a matrix of these dimensions in which the value of each cell is the product of the row number x column number.

matDims <- function(nr=sample(1:20,size=1),nc=sample(1:20,size=1)){
  mat = matrix(0,nr=nr,nc=nc)
  
 for (r in 1:nr){
      for (c in 1:nc){
      mat[r,c] = r*c
    }}
    return(mat)
}  

matDims()
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
##  [1,]    1    2    3    4    5    6    7    8    9    10    11    12    13
##  [2,]    2    4    6    8   10   12   14   16   18    20    22    24    26
##  [3,]    3    6    9   12   15   18   21   24   27    30    33    36    39
##  [4,]    4    8   12   16   20   24   28   32   36    40    44    48    52
##  [5,]    5   10   15   20   25   30   35   40   45    50    55    60    65
##  [6,]    6   12   18   24   30   36   42   48   54    60    66    72    78
##  [7,]    7   14   21   28   35   42   49   56   63    70    77    84    91
##  [8,]    8   16   24   32   40   48   56   64   72    80    88    96   104
##  [9,]    9   18   27   36   45   54   63   72   81    90    99   108   117
## [10,]   10   20   30   40   50   60   70   80   90   100   110   120   130
## [11,]   11   22   33   44   55   66   77   88   99   110   121   132   143
## [12,]   12   24   36   48   60   72   84   96  108   120   132   144   156
## [13,]   13   26   39   52   65   78   91  104  117   130   143   156   169
## [14,]   14   28   42   56   70   84   98  112  126   140   154   168   182
## [15,]   15   30   45   60   75   90  105  120  135   150   165   180   195
##       [,14] [,15] [,16] [,17] [,18] [,19]
##  [1,]    14    15    16    17    18    19
##  [2,]    28    30    32    34    36    38
##  [3,]    42    45    48    51    54    57
##  [4,]    56    60    64    68    72    76
##  [5,]    70    75    80    85    90    95
##  [6,]    84    90    96   102   108   114
##  [7,]    98   105   112   119   126   133
##  [8,]   112   120   128   136   144   152
##  [9,]   126   135   144   153   162   171
## [10,]   140   150   160   170   180   190
## [11,]   154   165   176   187   198   209
## [12,]   168   180   192   204   216   228
## [13,]   182   195   208   221   234   247
## [14,]   196   210   224   238   252   266
## [15,]   210   225   240   255   270   285