gpt4 book ai didi

r - 如何将 boxplot 的数字输出(带有 plot=FALSE)变成可用的东西?

转载 作者:行者123 更新时间:2023-12-04 04:49:15 25 4
gpt4 key购买 nike

我成功地使用 boxplot 函数来生成...箱线图。现在我需要生成包含 boxplot 计算的统计数据的表,以便创建绘图。

我通过使用 plot=FALSE 选项来做到这一点。

问题是这产生了一种相当奇怪的格式的数据,我根本无法做任何事情。下面是一个例子:

structure(list(stats = structure(c(178.998262143545, 182.227431564442, 
202.108456373209, 220.375358994654, 221.990406228232, 216.59986775699,
217.054997032148, 228.509462713206, 267.070720949859, 284.832378859975,
189.864120937198, 201.876421960518, 219.525439081472, 234.260088973545,
279.343359793024, 209.472617639903, 209.526516071858, 214.785213079737,
230.027361556731, 240.0647114578, 202.057148813419, 207.375619207685,
220.093663781351, 226.246698737471, 240.343646265795), .Dim = c(5L,
5L)), n = c(4, 6, 8, 4, 8), conf = structure(c(171.971593703341,
232.245319043076, 196.247705331772, 260.771220094641, 201.435457751239,
237.615420411705, 198.589545146688, 230.980881012787, 209.552007821332,
230.635319741371), .Dim = c(2L, 5L)), out = numeric(0), group = numeric(0),
names = c("U", "UM", "M", "LM", "L")), .Names = c("stats", "n", "conf", "out", "group",
"names"))

我想要的是每个统计数据的表格——最小值、最大值、中位数和四分位数——以及它们对每个组的值(“名称”中的值)。

有人可以帮我解决这个问题吗?我是一个 R 初学者。

提前致谢!

最佳答案

boxplot在 R 中返回一个名为 list 的结构体.

列表或多或少是一个数据容器,您可以在其中按名称引用元素。
如果你这样做 A <- boxplot(...) ,您可以访问 namesA$names , confA$conf , 等等。

所以,看着 boxplot帮助文件 ?boxplotValue: (它告诉你 boxplot 返回什么),我们看到它返回一个包含以下组件的列表:

   stats: a matrix, each column contains the extreme of the lower
whisker, the lower hinge, the median, the upper hinge and the
extreme of the upper whisker for one group/plot. If all the
inputs have the same class attribute, so will this component.
n: a vector with the number of observations in each group.
conf: a matrix where each column contains the lower and upper
extremes of the notch.
out: the values of any data points which lie beyond the extremes
of the whiskers.
group: a vector of the same length as ‘out’ whose elements indicate
to which group the outlier belongs.
names: a vector of names for the groups.

因此,每个统计数据的表格都在 A$stats 中,每列属于一个组,包含最小值、下四分位数、中位数、上四分位数和最大值。

你可以这样做:
A <- boxplot(...)
mytable <- A$stats
colnames(mytable)<-A$names
rownames(mytable)<-c('min','lower quartile','median','upper quartile','max')
mytable

返回(对于 mytable ):
                      U       UM        M       LM        L
min 178.9983 216.5999 189.8641 209.4726 202.0571
lower quartile 182.2274 217.0550 201.8764 209.5265 207.3756
median 202.1085 228.5095 219.5254 214.7852 220.0937
upper quartile 220.3754 267.0707 234.2601 230.0274 226.2467
max 221.9904 284.8324 279.3434 240.0647 240.3436

那么你可以引用它像 mytable['min','U'] .

关于r - 如何将 boxplot 的数字输出(带有 plot=FALSE)变成可用的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com