Mean of variable by two factors

后端 未结 2 1031
说谎
说谎 2021-02-05 23:18

I have the following data:

a <- c(1,1,1,1,2,2,2,2)
b <- c(2,4,6,8,2,3,4,1)
c <- factor(c(\"A\",\"B\",\"A\",\"B\",\"A\",\"B\",\"A\",\"B\"))
df <- data         


        
2条回答
  •  甜味超标
    2021-02-05 23:53

    Personally I would use aggregate:

    aggregate(length ~ sp, data = df, FUN= "mean" )
    # by species only
    #     sp length
    #1  1    5.0
    #2  2    2.5
    
    aggregate(length ~ sp + method, data = df, FUN= "mean" )
        # by species and method
    #  sp method length
    #1  1      A      4
    #2  2      A      3
    #3  1      B      6
    #4  2      B      2
    

    for everything together you may want:

    aggregate(length ~ method, data = df, function(x) c(m = mean(x), counts = length(x)) )
    
    # counts and mean for each method
    #  method length.m length.counts
    #1      A      3.5           4.0
    #2      B      4.0           4.0
    

提交回复
热议问题