【发布时间】:2022-01-24 00:22:10
【问题描述】:
我正在尝试计算该数据集在每列中的样本数、均值、标准差、变异系数、上下 95% 置信限和四分位数,并将其放入新的数据框。
下面的数字不一定都是正确的,我没有全部填写,只是提供了一个例子。这些值将用于创建箱线图,因此需要四分位数。行和列最终将成为标题。请参见下面的示例。
结构如下:
B1 <- c(8, 6, 13, 6, 27, 104, 18, 3)
B2 <- c(2, 13, 1, 64, 127, 24, 4, 3)
B3 <- c(8, 16, 113, 680, 227, 310, 138, 30)
B4 <- c(238, 46, 613, 69, 7, 14, 4, 8)
x <- data.frame(B1, B2, B3, B4)
> head(x)
B1 B2 B3 B4
1 8 2 8 238
2 6 13 16 46
3 13 1 113 613
4 6 64 680 69
5 27 127 227 7
6 104 24 310 14
期望的输出:
> y
B1 B2 B3 B4
n 8 8 8 8
mean 23 30 190 125
Stand dev 5 2 34 2
CoeffofVariation 0.3 0.4 0.7 1.3
LowerConfInterval 2 20 35 45
UpperConfInterval 50 120 122 120
LowerQuartile
Median
Upper Quantile
Inter Quartile Range
Minimum
Maximum
Regression equation
【问题讨论】:
-
编写一个函数,该函数返回一个带有每个感兴趣的统计数据的命名向量。然后使用
sapply循环遍历data.frame。myFunc <- function(x) c(mean=mean(x), n=length(x), median=median(x))然后sapply(dat, myFunc)。将其包装在data.frame中以获取 data.frame 而不是矩阵。 -
"...这些值将用于创建箱线图" 那么为什么不直接使用 ggplot2 的
geom_boxplot呢? ggplot2.tidyverse.org/reference/geom_boxplot.html