Mean by factor by level

前端 未结 4 2064
小鲜肉
小鲜肉 2021-02-07 05:01

Maybe this is simple but I can\'t find answer on web. I have problem with mean calculation by factors by level. My data looks typicaly:

factor, value
a,1
a,2
b,1         


        
4条回答
  •  旧时难觅i
    2021-02-07 05:38

    Just for fun posting the data.table solution although you probably should do what @lukeA suggested

    library(data.table) 
    A <- setDT(df)[factor == "a", mean(value)]
    ## [1] 1.5
    

提交回复
热议问题