Calculate Group Mean and Overall Mean

DGenchev

I have a data frame with several variables I want to get the means of and a variable I want to group by. Then, I would like to get the proportion of each group's mean to the overall mean.

I have put together the following, but it is clumsy.

How would you go about it using dplyr or data.table? Bonus points for the option to return both the intermediate step (group and overall mean) and the final proportions.

library(tidyverse)

set.seed(1)
Data <- data.frame(
  X1 = sample(1:10),
  X2 = sample(11:20),
  X3 = sample(21:30),
  Y = sample(c("yes", "no"), 10, replace = TRUE)
)

groupMeans <- Data %>% 
  group_by(Y) %>%
  summarize_all(funs(mean))

overallMeans <- Data %>% 
  select(-Y) %>% 
  summarize_all(funs(mean))

index <- sweep(as.matrix(groupMeans[, -1]), MARGIN = 2,  as.matrix(overallMeans), FUN = "/")
Nar

here is one more dplyr solution

index <- as.data.frame(Data %>% 
    group_by(Y) %>%
    summarise_all(mean) %>%
    select(-Y)  %>%
    rbind(Data %>% select(-Y) %>% summarise_all(mean))%>%
    mutate_all(funs( . / .[3])))[1:2,]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate overall mean of multiple columns by group

From Dev

Calculate mean by group with dplyr

From Dev

Calculate mean for subgroup within group

From Dev

How to calculate the mean of a new group?

From Dev

Calculating the overall mean, not a mean of means

From Dev

How to calculate mean in group by another group?

From Dev

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

From Dev

How to calculate a mean by group in a JS Array?

From Dev

for loop to calculate mean by group (also ignore NA)

From Dev

Group by and calculate mean / average of properties in a Javascript array

From Dev

Group the data based on binary, and calculate mean, sd

From Dev

Group multiindex dataframes by labels to calculate mean

From Dev

How to calculate mean spatial location by group

From Dev

Group list by a given element and calculate the mean

From Dev

Calculate mean by group using dplyr package

From Dev

Calculate the mean of a nested group if the condition holds

From Dev

Calculate mean difference per row and per group

From Dev

How to group specific items in a column and calculate the mean

From Dev

Calculate group mean while excluding current observation using dplyr

From Dev

Python: calculate mean, std within a group and keep the old columns

From Dev

Subset / group by pandas Data Frame to calculate mean and apply to missing values

From Dev

calculate a weighted mean by group with dplyr (and replicate other approaches)

From Dev

Pandas:Calculate mean of a group of n values of each columns of a dataframe

From Python

How to group by two columns, calculate weighted mean, return DataFrame, in Python

From Dev

Pandas/Python groupby and then calculate mean for another column within each group

From Dev

Calculate group mean with the same grouping factors several times

From Dev

Calculate conditional mean in R with dplyr (like group by in SQL)

From Dev

Calculate the mean number of days between dates per group

From Dev

Pandas - Calculate mean for group over expanding window of dates

Related Related

  1. 1

    Calculate overall mean of multiple columns by group

  2. 2

    Calculate mean by group with dplyr

  3. 3

    Calculate mean for subgroup within group

  4. 4

    How to calculate the mean of a new group?

  5. 5

    Calculating the overall mean, not a mean of means

  6. 6

    How to calculate mean in group by another group?

  7. 7

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

  8. 8

    How to calculate a mean by group in a JS Array?

  9. 9

    for loop to calculate mean by group (also ignore NA)

  10. 10

    Group by and calculate mean / average of properties in a Javascript array

  11. 11

    Group the data based on binary, and calculate mean, sd

  12. 12

    Group multiindex dataframes by labels to calculate mean

  13. 13

    How to calculate mean spatial location by group

  14. 14

    Group list by a given element and calculate the mean

  15. 15

    Calculate mean by group using dplyr package

  16. 16

    Calculate the mean of a nested group if the condition holds

  17. 17

    Calculate mean difference per row and per group

  18. 18

    How to group specific items in a column and calculate the mean

  19. 19

    Calculate group mean while excluding current observation using dplyr

  20. 20

    Python: calculate mean, std within a group and keep the old columns

  21. 21

    Subset / group by pandas Data Frame to calculate mean and apply to missing values

  22. 22

    calculate a weighted mean by group with dplyr (and replicate other approaches)

  23. 23

    Pandas:Calculate mean of a group of n values of each columns of a dataframe

  24. 24

    How to group by two columns, calculate weighted mean, return DataFrame, in Python

  25. 25

    Pandas/Python groupby and then calculate mean for another column within each group

  26. 26

    Calculate group mean with the same grouping factors several times

  27. 27

    Calculate conditional mean in R with dplyr (like group by in SQL)

  28. 28

    Calculate the mean number of days between dates per group

  29. 29

    Pandas - Calculate mean for group over expanding window of dates

HotTag

Archive