7

我成功地使用该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 初学者。

提前致谢!

4

3 回答 3

24

boxplot返回 R 中称为 a 的结构list

列表或多或少是一个数据容器,您可以在其中按名称引用元素。如果这样做A <- boxplot(...),您可以访问nameswith A$namesconfwithA$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'].

于 2012-01-13T01:46:07.853 回答
4

如果您真的想要数据的分位数而不是箱线图数字,quantile那么直接使用将是我的选择(如果您仔细阅读稍后所做的事情,它会更容易阅读)。

quantile (x, probs = c (0, .25, .5,.75, 1))

quantile它本身不适用于组,但您可以将其组合起来,aggregate以便为参数中给出的每个组调用它by(需要是一个列表,因此您可以在此处组合几个分组因素):

aggregate (chondro$x, by = list (chondro$clusters), 
           FUN = quantile, probs = c (0, .25, .5,.75, 1))

结果:

   Group.1   x.0%  x.25%  x.50%  x.75% x.100%
1  matrix -11.55  -6.55   5.45  14.45  22.45
2  lacuna -11.55  -2.55   4.45  10.45  22.45
3    cell  -8.55  -1.55  11.45  15.45  20.45

如果你真的想要箱线图数字(例如胡须走多远),请查看? fivenumand ? boxplot.stats

于 2012-01-13T10:25:39.513 回答
1

其他人已经回答了关于boxplot函数返回对象的具体问题,我只想补充一点,如果你想了解一般的返回对象,那么你应该真正了解列表以及如何使用str函数,这通常会给你更多一个对象的有意义的视图然后你上面显示的。包中还有一个TkListView功能,TeachingDemos可以对列表和其他对象进行更具交互性的探索。使用strandnames和子集(参见 help("["))将让您了解返回对象中的内容(创建对象的函数的帮助页面也是一个很好的起点)以及如何访问你想要的碎片。

于 2012-01-13T16:50:52.687 回答