这是用于计算描述性统计的代码
densities <- abs(rnorm(100,mean = 15000, sd = 11600)) #just a vector of nonzero normal data
#run through descriptive statistics
function.names <- c("mean","quantile","IQR","sd","max","min","median")
for (i in 1:length(function.names)){
assign("fun1", get(function.names[i]) )
assign(paste("data_", function.names[i], sep=""), fun1(densities))
rm(fun1) #start over
}
range <- max(densities)-min(densities) #range
pearson_mode_skewness = (mean(densities)- median(densities)/sd(densities))
df_desc <- data.frame(function.names, paste("data_", function.names, sep="")) #plot in a dataframe/cell array
df_desc
> df_desc
function.names paste..data_...function.names..sep......
1 mean data_mean
2 quantile data_quantile
3 IQR data_IQR
4 sd data_sd
5 max data_max
6 min data_min
7 median data_median
==========
我正在寻找遍历所有这些描述性统计数据。以后可能需要包含更多的函数,所以我把它做成了一个可扩展的函数向量。如上所示,如何制作有效的摘要幻灯片来承载第二列中的值。我想要每个函数的值(对于多结果统计,最好将其截断为字符串。)我计划将其转换为用于多个密度向量(及其子样本)的函数。
\ 提前致谢!
编辑:基于答案的当前工作代码
#DESCRIPTIVE STATS
descriptive_table <- function(data){
funlist <- list(mean,quantile,IQR,sd,max,min,median)
temp <- cbind(c("mean","quantile","IQR","sd","max","min","median"), lapply(funlist, function(fn) fn(data)))
colnames(temp) <- c("Statistic", "Value")
descriptives <- rbind(temp, c("range",max(data)-min(data)),
c("Pearson Mode Skewness", (mean(data)- median(data)/sd(data))) )
print(descriptives)
}