6

我有点习惯了,melt而且cast这一次我正在寻找整洁的单线。

require(reshape)
# first I melt some data:
m <- melt(mtcars, id.vars = c("cyl", "am"), measure.vars = "hp")
# then cast it:
cast(m, cyl + am ~ ., each(min, mean, sd, max))
  cyl am min      mean       sd max
1   4  0  62  84.66667 19.65536  97
2   4  1  52  81.87500 22.65542 113
3   6  0 105 115.25000  9.17878 123
4   6  1 110 131.66667 37.52777 175
5   8  0 150 194.16667 33.35984 245
6   8  1 264 299.50000 50.20458 335

这是可能的ddply吗?我很渴望单线。我试过:

ddply(mtcars, cyl + am ~ hp, each(min, max))
   cyl am  hp min   max
1    4  0  62   0 146.7
2    4  0  95   0 140.8
3    4  0  97   0 120.1
4    4  1  52   1  75.7
5    4  1  65   1  71.1
6    4  1  66   1  79.0
7    4  1  91   0 120.3
8    4  1  93   1 108.0
9    4  1 109   1 121.0
10   4  1 113   1 113.0
11   6  0 105   0 225.0
12   6  0 110   0 258.0
13   6  0 123   0 167.6
14   6  1 110   0 160.0
15   6  1 175   0 175.0
16   8  0 150   0 318.0
17   8  0 175   0 400.0
18   8  0 180   0 275.8
19   8  0 205   0 472.0
20   8  0 215   0 460.0
21   8  0 230   0 440.0
22   8  0 245   0 360.0
23   8  1 264   0 351.0
24   8  1 335   0 335.0

当然,这是可行的,但不是通过hpcyl总结am。我已经有一段时间没有使用了plyrreshape所以我有点失去了肌肉......所以......借口-moi pour une question triviale... =/

4

3 回答 3

12

summmarize可能是你的朋友:

ddply(m, c("cyl", "am"), summarize
      , min = min(value)
      , mean = mean(value)
      , sd = sd(value)
      , max = max(value)
)

  cyl am min      mean       sd max
1   4  0  62  84.66667 19.65536  97
2   4  1  52  81.87500 22.65542 113
3   6  0 105 115.25000  9.17878 123
4   6  1 110 131.66667 37.52777 175
5   8  0 150 194.16667 33.35984 245
6   8  1 264 299.50000 50.20458 335
于 2011-10-10T19:34:33.223 回答
6

使用plyr

> require(plyr)
> ddply(mtcars,c("cyl","am"),summarise, min=min(hp), mean=mean(hp), sd=sd(hp), max=max(hp))
  cyl am min      mean       sd max
1   4  0  62  84.66667 19.65536  97
2   4  1  52  81.87500 22.65542 113
3   6  0 105 115.25000  9.17878 123
4   6  1 110 131.66667 37.52777 175
5   8  0 150 194.16667 33.35984 245
6   8  1 264 299.50000 50.20458 335
于 2011-10-10T19:34:29.927 回答
3
> ddply(mtcars, .(cyl, am), summarise, 
        min=min(hp), mean=mean(hp), sd=sd(hp), max=max(hp))
  cyl am min      mean       sd max
1   4  0  62  84.66667 19.65536  97
2   4  1  52  81.87500 22.65542 113
3   6  0 105 115.25000  9.17878 123
4   6  1 110 131.66667 37.52777 175
5   8  0 150 194.16667 33.35984 245
6   8  1 264 299.50000 50.20458 335

我不知道如何避免不得不两次命名每个函数,虽然......

于 2011-10-10T19:36:19.743 回答