R中的汇总函数给出如下输出
Variable 1
Min. :-2.192e+09
1st Qu.:0.000e+00
Median :2.632e+05
Mean :7.933e+06
3rd Qu.:4.668e+06
Max. :1.124e+09
NA's :277
我希望以下列格式获得输出:
Min 1st Qu Median Mean 3rd Qu Max NA's
Variable 1 - - - - - - -
Variable 2 - - - - - - -
我该怎么做并导出到Excel?
答案 0 :(得分:3)
如果我是对的,您想要将向量的摘要信息写入Excel吗?如果是这样,这可能适合您(包 readr ,或者您可以使用write.csv()
):
tmp <- data.frame(
var1 = runif(100, min = 5, max = 50),
var2 = c(runif(99, min = 50, max = 150), NA),
var3 = runif(100, min = 150, max = 250)
)
tmp <- as.data.frame(t(do.call(cbind, lapply(tmp, function(x) {
# save summary information
v <- as.vector(summary(x))
# if vector has no NAs, manually add 0, to ensure equal
# length of vectors for column bind
if (length(v) < 7) v <- c(v, 0)
# return summary
v
}))))
colnames(tmp) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.", "NA's")
# result so far:
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## var1 5.097 15.12 26.83 26.46 36.89 49.75 0
## var2 50.900 83.08 104.50 103.80 129.00 149.20 1
## var3 151.200 166.80 200.30 197.00 223.70 248.50 0
readr::write_excel_csv(tmp, "summary.csv")