为R中的各种变量运行已定义的函数

时间:2017-10-14 18:27:07

标签: r function

我已经为描述性统计定义了一个函数,即

descriptive_statistics<-function(obj){
  n<-length(obj)
  cat("mean=" ,mean(obj) , "\n")
  cat("s.d.=",sd(obj),"\n")
  cat("min=" ,min(obj ) , "\n")
  cat("1. Quartil=" ,quantile(obj, 0.25) , "\n")
  cat("median=",median(obj),"\n")
  cat("3. Quartil=" ,quantile(obj, 0.75) , "\n")  
  cat ( "max= " , max (obj ) , "\n" )  
    skew<-(sum((obj-mean(obj))^3)/n)/(sum((obj-mean(obj))^2)/n)^(3/2)
  cat("skewness=",skew,"\n")
  kurt<-n*sum((obj-mean(obj))^4)/(sum((obj-mean(obj))^2)^2)
  cat("kurtosis=",kurt,"\n")
  Stat<-n*skew^2/6+n*(kurt - 3)^2/24
}

对于我在命令中输入的任何变量,它给出了这个输出:

descriptive_statistics(data$ewz_2016)
mean= 232024.4 
s.d.= 383172 
min= 36514 
1. Quartil= 81632 
median= 121705 
3. Quartil= 230405.5 
max= 3516100 
skewness= 5.930454 
kurtosis= 46.42959

我的愿望是同时在命令中放入各种变量(例如第3-25列中的变量),然后将表格复制到Excel进行格式化。 有人有建议吗?

1 个答案:

答案 0 :(得分:0)

我会更改函数descriptive_statistics以返回以命名向量的形式计算的值。在下面的函数中没有打印任何东西,它只是计算这些值。如果需要,您可以将它们和它的两个版本组合在一起。

descriptive_statistics2 <- function(obj){
  n <- length(obj)
  skew<-(sum((obj-mean(obj))^3)/n)/(sum((obj-mean(obj))^2)/n)^(3/2)
  kurt <- n*sum((obj-mean(obj))^4)/(sum((obj-mean(obj))^2)^2)
  Stat <- n*skew^2/6+n*(kurt - 3)^2/24
  c(mean = mean(obj),
    s.d. = sd(obj),
    min = min(obj),
    Quartil = quantile(obj, 0.25),  # Note: no need to say it's quartile 1 or 3
    median = median(obj),
    Quartil = quantile(obj, 0.75),  # quantile() will append a 25% or 75%
    max = max(obj),
    skewness = skew,
    kurtosis = kurt,
    Stat = Stat
  )
}

现在用一些数据测试它。

set.seed(8085)  # make it reproducible
n <- 1e2
dat <- as.data.frame(matrix(rnorm(25*n), ncol = 25))

result <- t(sapply(dat[3:25], descriptive_statistics2))
head(result)
#          mean      s.d.       min Quartil.25%      median Quartil.75%      max
#V3  0.04496632 0.9454383 -2.114690  -0.6352870  0.13869333   0.6789609 1.901803
#V4  0.08136469 1.0594723 -1.955842  -0.6922518 -0.03305246   0.8274921 2.653876
#V5 -0.05899082 1.0225392 -3.031083  -0.6536158 -0.16813752   0.6497042 2.053943
#V6  0.13282456 0.9746959 -1.715005  -0.5399552  0.05815323   0.8383848 2.308354
#V7  0.03935582 1.1629889 -2.669025  -0.6569735 -0.01753087   0.6976987 3.277311
#V8 -0.08203328 1.1111241 -2.755361  -0.9619138 -0.08806534   0.8730580 2.547580
#      skewness kurtosis      Stat
#V3 -0.20061108 2.414033 2.1014041
#V4  0.32427896 2.455259 2.9890412
#V5 -0.21501670 2.758610 1.0133248
#V6  0.16328790 2.322679 2.3558991
#V7  0.09352129 2.977775 0.1478287
#V8 -0.05664655 2.509671 1.0552417

最后,要将结果导出到Excel,请使用write.csv,如果您的国家/地区使用句点作为小数点,或write.csv2使用逗号。

write.csv(result, file = "SCW16.csv", row.names = FALSE)