English 中文(简体)
Mend reshape-based habits with plyr: melt/cast vs. ddply
原标题:Mend reshape-based habits with plyr: melt/cast vs. ddply

I m kind-of used to do melt and cast all the time, and this time I m looking for neat one-liner.

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

可在<代码>dply上查阅。 或 s? 我急切希望有一个一线。 我尝试:

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

当然,这项工作不是按总和分列的<代码>hp,通过<编码>cyl和am。 缩略语 因此,丧失了我的肌肉......excuse-moi pour une question triviale...=/

最佳回答

可能是你的朋友:

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
问题回答

Using 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
> 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

I m not sure how to avoid having to name each function twice, though...





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签