这是用于计算描述性统计数据的代码
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)
}
答案 0 :(得分:2)
这是一个更好的策略:
> funlist <- list(mean,quantile,IQR,sd,max,min,median)
> vals <-lapply(funlist, function(fn) fn(densities) )
[[1]]
[1] 16291.04
[[2]]
0% 25% 50% 75% 100%
195.831 7080.740 16736.867 23635.907 46913.716
[[3]]
[1] 16555.17
[[4]]
[1] 10831.34
[[5]]
[1] 46913.72
[[6]]
[1] 195.831
[[7]]
[1] 16736.87
如果您想稍后将这些功能命名为运气,那么因为您(或我)没有给它们命名,所以这可能会更加小心:
funlist2 <- list(mean=mean,quantile=quantile,IQR=IQR,
sd=sd,max=max,min=min,median=median)
vals <- lapply(funlist2, function(fn) fn(densities) )
names(vals) <- names( funlist2)