Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列添加到R摘要_R_Statistics - Fatal编程技术网

将列添加到R摘要

将列添加到R摘要,r,statistics,R,Statistics,我有一个向量的R摘要: summary(vector) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.000 1.000 2.000 6.699 6.000 559.000 我想添加一列标准偏差: SomethingNew(vector) Min. 1st Qu. Median Mean 3rd Qu. Max. Std.Dev. 1.000 1.000 2.000 6.699 6.000 5

我有一个向量的R摘要:

summary(vector)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
1.000   1.000   2.000   6.699   6.000 559.000 
我想添加一列标准偏差:

SomethingNew(vector)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.   Std.Dev.
1.000   1.000   2.000   6.699   6.000 559.000  17.02
最后一列的公式为

round(sd(vector),2)

但我不知道如何将其添加到同一显示中的摘要数据框中。感谢您的帮助,干杯。

这里有一种方法:

vec <- 1:10 # an example vector
summ <- summary(vec) # create the summary

summ["Std.Dev."] <- round(sd(vec),2) # add the new value
试试这个

> set.seed(1)
> vector <- rnorm(100, 20, 5)
> c(summary(vector), sd=sd(vector))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max.        sd 
 8.927000 17.530000 20.570000 20.540000 23.460000 32.010000  4.490997 

尝试编写一个新函数来完成此操作。我写了一篇关于如何编写简单函数()的简短概述,您可以将其用作资源

基本上,您需要以下内容:

mySummary <- function(vector, na.rm = FALSE, round = 2){
  results <- c(summary(vector), 'Std. Dev' = round(sd(vector, na.rm), 2))
  return(results)
}

mySummary我建议在这里舍入时使用
signif
而不是
round
,您通常更感兴趣的是获得n个有效数字,而不是舍入到指定的小数位数。
summary
使用
signif
的digits参数
max(3,getOption(“digits”)-3)
> round(c(summary(vector), sd=sd(vector)), 2)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.      sd 
   8.93   17.53   20.57   20.54   23.46   32.01    4.49 
mySummary <- function(vector, na.rm = FALSE, round = 2){
  results <- c(summary(vector), 'Std. Dev' = round(sd(vector, na.rm), 2))
  return(results)
}