-1

这是用于计算描述性统计的代码

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)
}
4

1 回答 1

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)
于 2013-06-14T22:56:35.840 回答