How to average and count in each group and creating a new table

前端 未结 1 365
情歌与酒
情歌与酒 2021-01-25 06:40

I have a Dataset, I want to calculate average of KPI, CPM and CPC column and count times column in each score group(1-10).

How to create a new table according results?

1条回答
  •  生来不讨喜
    2021-01-25 07:23

    assuming your data is in a data.frame called df, do you just want this?

    library(data.table)
    setDT(df)[ ,.(lapply(.SD, mean), .N), by = score, .SDcols = c("KPI", "CPM", "CPC")]
    

    or do you want this?

    library(dplyr)
    group_by(df, score) %>%
        summarise(Mean_KPI = mean(KPI),
                  Mean_CPC = mean(CPC),
                  Mean_CPM = mean(CPM),
                  Sum_times = sum(times))
    

    0 讨论(0)
提交回复
热议问题