operationareamat.R 4.14 KB
Newer Older
1 2 3 4 5 6 7
#' Function for getting the mean value of an area on a matrix.
#'
#' @description A function for calculating the mean value of an area of a matrix.
#' For example if we want to get the mean value of part of a 10x10 matrix, starting from
#' 2nd column, 1st row to 3rd row, 5th column, we'll get the values of this smaller matrix
#' (3x4 to be precise) and then we'll calculate the mean of all of them.
#'
8
#' @usage meanareamat(m, rowmin=1, rowmax, colmin=1, colmax)
9 10 11
#'
#' @param m A matrix object
#'
12
#' @param rowmin The first row for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
13
#'
14
#' @param rowmax The last row for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
15
#'
16
#' @param colmin The first column for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
17
#'
18
#' @param colmax The last column for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
19
#'
20 21 22 23 24 25
#' @return The mean value of the subsetted area of matrix m
#'
#' @examples
#' m <- matrix(rnorm(100, mean = 0, sd = 1), nrow = 10, ncol = 10)
#' meanarea <- meanareamat(m, rowmin = 1, rowmax = 3, colmin = 2, colmax = 5)

26
meanareamat <- function(m, rowmin=1, rowmax, colmin=1, colmax){
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  m_n <- m[rowmin:rowmax,colmin:colmax]

  mean_m <- mean(m_n)

  rm(m_n)

  return(mean_m)

}

#' Function for getting the median value of an area on a matrix.
#'
#' @description A function for calculating the median value of an area of a matrix.
#' For example if we want to get the median value of part of a 10x10 matrix, starting from
#' 2nd column, 1st row to 3rd row, 5th column, we'll get the values of this smaller matrix
#' (3x4 to be precise) and then we'll calculate the median of all of them.
#'
45
#' @usage medianareamat(m, rowmin=1, rowmax, colmin=1, colmax)
46 47 48
#'
#' @param m A matrix object
#'
49
#' @param rowmin The first row for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
50
#'
51
#' @param rowmax The last row for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
52
#'
53
#' @param colmin The first column for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
54
#'
55
#' @param colmax The last column for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
56
#'
57 58 59 60 61 62
#' @return The median value of the subsetted area of matrix m
#'
#' @examples
#' m <- matrix(rnorm(100, mean = 0, sd = 1), nrow = 10, ncol = 10)
#' medianarea <- medianareamat(m, rowmin = 1, rowmax = 3, colmin = 2, colmax = 5)

63
medianareamat <- function(m, rowmin=1, rowmax, colmin=1, colmax){
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  m_n <- m[rowmin:rowmax,colmin:colmax]

  median_m <- median(m_n)

  rm(m_n)

  return(median_m)

}

#' Function for getting the standard deviation value of an area on a matrix.
#'
#' @description A function for calculating the standard deviation value of an area of a matrix.
#' For example if we want to get the standard deviation value of part of a 10x10 matrix, starting from
#' 2nd column, 1st row to 3rd row, 5th column, we'll get the values of this smaller matrix
#' (3x4 to be precise) and then we'll calculate the standard deviation of all of them.
#'
82
#' @usage sdareamat(m, rowmin=1, rowmax, colmin=1, colmax)
83 84 85
#'
#' @param m A matrix object
#'
86
#' @param rowmin The first row for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
87
#'
88
#' @param rowmax The last row for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
89
#'
90
#' @param colmin The first column for subsetting. Default value is 1.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
91
#'
92
#' @param colmax The last column for subsetting.
Ahmad Alkadri's avatar
Ahmad Alkadri committed
93
#'
94 95 96 97 98 99
#' @return The standard deviation value of the subsetted area of matrix m
#'
#' @examples
#' m <- matrix(rnorm(100, mean = 0, sd = 1), nrow = 10, ncol = 10)
#' sdarea <- sdareamat(m, rowmin = 1, rowmax = 3, colmin = 2, colmax = 5)

100
sdareamat <- function(m, rowmin=1, rowmax, colmin=1, colmax){
101 102 103 104 105 106 107 108 109 110

  m_n <- m[rowmin:rowmax,colmin:colmax]

  sd_m <- sd(m_n)

  rm(m_n)

  return(sd_m)

}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

#' Function to subset a matrix.
#'
#' @description A function for subsetting a matrix.
#'
#' @usage subsetmat(m, rowmin=1, rowmax, colmin=1, colmax)
#'
#' @param m A matrix object
#'
#' @param rowmin The first row for subsetting. Default value is 1.
#'
#' @param rowmax The last row for subsetting.
#'
#' @param colmin The first column for subsetting. Default value is 1.
#'
#' @param colmax The last column for subsetting.
#'
#' @return The standard deviation value of the subsetted area of matrix m
#'
#' @examples
#' m <- matrix(rnorm(100, mean = 0, sd = 1), nrow = 10, ncol = 10)
#' m_part <- subsetmat(m, rowmin = 1, rowmax = 3, colmin = 2, colmax = 5)

subsetmat <- function(m, rowmin=1, rowmax, colmin=1, colmax){

  m_n <- m[rowmin:rowmax,colmin:colmax]

  return(m_n)

}