Multiple functions in a single tapply or aggregate statement

前端 未结 4 441
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 22:27

Is it possible to include two functions within a single tapply or aggregate statement?

Below I use two tapply statements and two aggregate statements: one for mean a

4条回答
  •  [愿得一人]
    2020-12-08 22:59

    But these should have:

    with(my.Data, aggregate(weight, list(age, sex), function(x) { c(MEAN=mean(x), SD=sd(x) )}))
    
    with(my.Data, tapply(weight, list(age, sex), function(x) { c(mean(x) , sd(x) )} ))
    # Not a nice structure but the results are in there
    
    with(my.Data, aggregate(weight ~ age + sex, FUN =  function(x) c( SD = sd(x), MN= mean(x) ) ) )
        age    sex weight.SD weight.MN
    1 adult female  3.535534 97.500000
    2 young female        NA 80.000000
    3 adult   male        NA 90.000000
    4 young   male        NA 75.
    

    The principle to be adhered to is to have your function return "one thing" which could be either a vector or a list but cannot be the successive invocation of two function calls.

提交回复
热议问题