如果我的分组变量是一个因素,如何生成按组汇总统计数据?

发布于 2024-10-15 00:33:53 字数 2751 浏览 5 评论 0原文

假设我想获取数据集 mtcars(基础 R 版本 2.12.1 的一部分)的一些汇总统计数据。 下面,我根据汽车所拥有的发动机气缸数对汽车进行分组,并采用 mtcars 中剩余变量的每组平均值。

> str(mtcars)
'data.frame': 32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
> ddply(mtcars, .(cyl), mean)
       mpg cyl     disp        hp     drat       wt     qsec        vs        am     gear
1 26.66364   4 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909 0.7272727 4.090909
2 19.74286   6 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286 0.4285714 3.857143
3 15.10000   8 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000 0.1428571 3.285714
      carb
1 1.545455
2 3.428571
3 3.500000

但是,如果我的分组变量恰好是一个因素,事情就会变得更加棘手。 ddply() 对因子的每个级别发出警告, 因为我们无法获取一个因子的mean()

> mtcars$cyl <- as.factor(mtcars$cyl)
> str(mtcars)
'data.frame': 32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
> ddply(mtcars, .(cyl), mean)
       mpg cyl     disp        hp     drat       wt     qsec        vs        am     gear
1 26.66364  NA 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909 0.7272727 4.090909
2 19.74286  NA 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286 0.4285714 3.857143
3 15.10000  NA 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000 0.1428571 3.285714
      carb
1 1.545455
2 3.428571
3 3.500000
Warning messages:
1: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
2: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
3: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
>

所以,我想知道我是否只是以错误的方式生成汇总统计数据。

通常如何生成按因素或按组汇总统计数据的数据结构(如平均值、标准差等)?我应该使用 ddply() 以外的东西吗?如果我可以使用 ddply(),我该如何避免在尝试获取分组因子的平均值时产生的错误?

Suppose I wanted to get some summary statistics on the dataset mtcars (part of base R version 2.12.1).
Below, I group the cars according to the number of engine cylinders they have and take the per-group means of the remaining variables in mtcars.

> str(mtcars)
'data.frame': 32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
> ddply(mtcars, .(cyl), mean)
       mpg cyl     disp        hp     drat       wt     qsec        vs        am     gear
1 26.66364   4 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909 0.7272727 4.090909
2 19.74286   6 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286 0.4285714 3.857143
3 15.10000   8 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000 0.1428571 3.285714
      carb
1 1.545455
2 3.428571
3 3.500000

But, if my grouping variable happens to be a factor things get trickier. ddply() throws a warning for each level of the factor,
since one can't take the mean() of a factor.

> mtcars$cyl <- as.factor(mtcars$cyl)
> str(mtcars)
'data.frame': 32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
> ddply(mtcars, .(cyl), mean)
       mpg cyl     disp        hp     drat       wt     qsec        vs        am     gear
1 26.66364  NA 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909 0.7272727 4.090909
2 19.74286  NA 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286 0.4285714 3.857143
3 15.10000  NA 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000 0.1428571 3.285714
      carb
1 1.545455
2 3.428571
3 3.500000
Warning messages:
1: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
2: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
3: In mean.default(X[[2L]], ...) :
  argument is not numeric or logical: returning NA
>

So, I'm wondering if I'm just going about generating summary statistics the wrong way.

How does one usually generate data structures of by-factor or by-group summary statistics (like means, standard deviations, etc.)? Should I be using something other than ddply()? If I can use ddply(), what can I do to avoid the errors that result when trying to take the mean of my grouping factor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2024-10-22 00:33:53

使用 numcolwise(mean)numcolwise 函数将其参数(函数)转换为仅对数字列进行操作的函数(并忽略分类/因子列)。

  > ddply(mtcars, .(cyl), numcolwise(mean))

      cyl      mpg     disp        hp     drat       wt     qsec        vs
    1   4 26.66364 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909
    2   6 19.74286 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286
    3   8 15.10000 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000
             am     gear     carb
    1 0.7272727 4.090909 1.545455
    2 0.4285714 3.857143 3.428571
    3 0.1428571 3.285714 3.500000

Use numcolwise(mean): the numcolwise function converts its argument (a function) into a function that operates only on numerical columns (and ignores the categorical/factor columns).

  > ddply(mtcars, .(cyl), numcolwise(mean))

      cyl      mpg     disp        hp     drat       wt     qsec        vs
    1   4 26.66364 105.1364  82.63636 4.070909 2.285727 19.13727 0.9090909
    2   6 19.74286 183.3143 122.28571 3.585714 3.117143 17.97714 0.5714286
    3   8 15.10000 353.1000 209.21429 3.229286 3.999214 16.77214 0.0000000
             am     gear     carb
    1 0.7272727 4.090909 1.545455
    2 0.4285714 3.857143 3.428571
    3 0.1428571 3.285714 3.500000
戴着白色围巾的女孩 2024-10-22 00:33:53

这里不是答案,而是观察。这不是 ddply() 本身的问题。看看这个。以下两者都可以很好地生成均值表:

aggregate(mtcars, by=list(mtcars$cyl), mean)
apply(mtcars, 2, function(col) tapply(col, INDEX=mtcars$cyl, FUN=mean))

但是在 mtcars$cyl <- as.factor(mtcars$cyl) 之后,以上都不起作用,因为 R 不知道如何采取一列因子的平均值。我们可以通过从传递给 mean() 的内容中删除该列(“cyl”是第 2 列)来避免这种情况:

aggregate(mtcars[ , -2], by=list(mtcars$cyl), mean)
apply(mtcars[ , -2], 2, function(col) tapply(col, INDEX=mtcars$cyl, FUN=mean))

但这非常笨重。

Not an answer here, but an observation. This is not an issue of ddply() per se. Look at this. The following both work fine to produce a table of means:

aggregate(mtcars, by=list(mtcars$cyl), mean)
apply(mtcars, 2, function(col) tapply(col, INDEX=mtcars$cyl, FUN=mean))

But after mtcars$cyl <- as.factor(mtcars$cyl) neither of the above work, because R doesn't know how to take the mean of a column of factors. We can avoid it by removing that column ("cyl" is column 2) from the things passed to mean():

aggregate(mtcars[ , -2], by=list(mtcars$cyl), mean)
apply(mtcars[ , -2], 2, function(col) tapply(col, INDEX=mtcars$cyl, FUN=mean))

But that's pretty clunky.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文