mean.k=function(x) { if (is.numeric(x)) round(mean(x, na.rm=TRUE), digits = 2) else "NA"}
median.k=function(x) { if (is.numeric(x)) round(median(x, na.rm=TRUE), digits = 2) else "NA"}
sd.k=function(x) { if (is.numeric(x)) round(sd(x, na.rm=TRUE), digits = 2) else "NA"}
min.k=function(x) { if (is.numeric(x)) round(min(x, na.rm=TRUE), digits = 2) else "NA"}
max.k=function(x) { if (is.numeric(x)) round(max(x, na.rm=TRUE), digits = 2) else "NA"}
#' Calculate summary statistics of a data frame or vector.
#'
#' @description Calculate summary statistics of a data frame or vector. Function is similar to \code{summary} but prints output in a more compact way, which can be also accessed through standard data frame calls. Additional outputs inculde standar deviation and mode. Original function can be found at https://thetarzan.wordpress.com/2011/05/24/summary-statistics-function-in-r-sumstats/
#'
#' @param x a data frame or a vector
#' @return summary statistics of \code{x}: count, mean, median, min, max, standard deviation, mode
#' @examples
#' des(iris)
#' des(rnorm(50))
#' @export
des<-function(x) {
if(is.numeric(x)){
sumtable<-data.frame(length(x),mean.k(x),median.k(x),sd.k(x),min.k(x),max.k(x),mode(x))
} else {
sumtable = cbind(as.matrix(colSums(!is.na(x))),
sapply(x,mean.k),
sapply(x,median.k),
sapply(x,sd.k),
sapply(x,min.k),
sapply(x,max.k),
sapply(x,mode))
sumtable=as.data.frame(sumtable)
}
names(sumtable)=c("n","Mean","Median","Std.Dev","Min","Max","Mode")
sumtable
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.