# Считает медиану входящей таблицы
Median <- function(df) {
#' Calculate median
#'
#' @description This function calculate the median
#' of whole input table.
#' @param df Numeric 2 dimension vector
#' @return Good way: one number - median of whole input table
#'
#' Bad way: -1 if input data are invalid
#' @details The inputs can be also numeric vector.
#' @examples
#' Median(data.frame)
#' Median(matrix)
#' Median(vector)
#' Median(List)
#' @export
# предобработка данных
df = as.data.frame(df)
temp = vector()
for (i in 1:nrow(df)){
temp <- append(temp, df[i,])
}
df <- unlist(temp)
n <- length(df)
if (n == 0){
return(-1)
}
# сортировать по неубыванию
df <- sort(df)
if (n %% 2 == 1){ # в выборке нечетное число эл-тов
result <- df[(n+1)/2]
} else { # в выборке четное число эл-тов
result <- (df[n/2] + df[n/2 + 1]) / 2
}
result <- unname(result)
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.