quick/elegant way to construct mean/variance summary table

后端 未结 8 2022
甜味超标
甜味超标 2020-12-13 19:45

I can achieve this task, but I feel like there must be a \"best\" (slickest, most compact, clearest-code, fastest?) way of doing it and have not figured it out so far ...

8条回答
  •  情书的邮戳
    2020-12-13 20:26

    I'm a bit puzzled. Does this not work:

    mvtab2 <- ddply(d,.(f1,f2,f3),
                summarise,y.mean = mean(y),y.var = var(y))
    

    This give me something like this:

       f1 f2  f3    y.mean       y.var
    1   A  a   I 0.6502307 0.095379578
    2   A  a  II 0.4876630 0.110796695
    3   A  a III 0.3102926 0.202805677
    4   A  b   I 0.3914084 0.058693103
    5   A  b  II 0.5257355 0.218631264
    

    Which is in the right form, but it looks like the values are different that what you specified.

    Edit

    Here's how to make your version with numcolwise work:

    mvtab2 <- ddply(subset(d,select=-c(z,rep)),.(f1,f2,f3),summarise,
                    y.mean = numcolwise(mean)(piece),
                    y.var = numcolwise(var)(piece)) 
    

    You forgot to pass the actual data to numcolwise. And then there's the little ddply trick that each piece is called piece internally. (Which Hadley points out in the comments shouldn't be relied upon as it may change in future versions of plyr.)

提交回复
热议问题