in R, how to calculate mean of all column, by group?

后端 未结 5 1705
挽巷
挽巷 2020-12-18 06:25

I need to get the mean of all columns of a large data set using R, grouped by 2 variables.

Lets try it with mtcars:

library(dplyr)
g_mtcars <- gro         


        
5条回答
  •  天涯浪人
    2020-12-18 07:00

    using data.table.(however you can't setDT(mtcars) because binding is locked. copy it to a different name like dt and try

     library(data.table)
     mt_dt = as.data.table(mtcars)
     mt_dt[ , lapply(.SD, mean) , by=c("cyl", "gear")]
    

提交回复
热议问题