How to calculate mean of grouped dataframe?

Rilcon42

I am trying to split a dataframe based on participant_number then calculate the grand mean of the specific columns Happiness and Joy (excluding column Lolz). Why does taking the mean of the column means result in:

Warning messages:
1: In mean.default(function (x, na.rm = FALSE, dims = 1L)  :
  argument is not numeric or logical: returning NA
2: In mean.default(function (x, na.rm = FALSE, dims = 1L)  :
  argument is not numeric or logical: returning NA

My code:

library(dplyr)
df<-data.frame(participant_number=c(1,1,1,2,2),Happiness=c(3,4,2,1,3),Joy=c(1,2,3,5,4),Lolz=c(3,3,3,3,3))

df%>%group_by(participant_number)%>%
select(Happiness,Joy)%>%
mutate(emoMean=mean(colMeans))

> df
  participant_number Happiness Joy Lolz
1                  1         3   1    3
2                  1         4   2    3
3                  1         2   3    3
4                  2         1   5    3
5                  2         3   4    3

GOAL

emoMean
participant_number ... emoMean
1                      2.5 (3+1+4+2+2+3)/6 #Note that this value does not include participant_number
1                      2.5
1                      2.5
2                      6.5
2                      6.5

Notes:

I tried to follow this as a potential solution but got completely lost

Psidom

For your specific case, you can just add the two columns together, take the mean and then divide it by two, since the two columns always have the same count:

df %>% group_by(participant_number) %>% mutate(emoMean = mean(Happiness + Joy)/2)

Source: local data frame [5 x 5]
Groups: participant_number [2]

  participant_number Happiness   Joy  Lolz emoMean
               <dbl>     <dbl> <dbl> <dbl>   <dbl>
1                  1         3     1     3    2.50
2                  1         4     2     3    2.50
3                  1         2     3     3    2.50
4                  2         1     5     3    3.25
5                  2         3     4     3    3.25

Note: At the mean time, by your definition of the mean of the first group, I think for the second group, it should be 3.25 instead of 6.5.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate mean in a dataframe?

From Dev

Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

From Dev

how to calculate cumsum with depreciation in a grouped dataframe?

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

How to calculate grand mean and standard deviation for the grouped table?

From Dev

how to calculate mean values of the past n years grouped by two variables

From Dev

calculate mean grouped by col number

From Dev

How to calculate mean of specific rows in python dataframe?

From Dev

How to ignore the zero value to calculate the mean in the dataframe

From Dev

How to calculate RMS for dataframe column grouped by two column labels?

From Dev

Calculate medians of rows in a grouped dataframe

From Java

How to calculate mean and standard deviation given a PySpark DataFrame?

From Dev

how to regroup, calculate the mean and generate new dataframe in R?

From Python

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

From Dev

How can I calculate the mean of a specific cell in a dataframe in Python?

From Dev

How to calculate difference on grouped df?

From Dev

Calculate quantile on grouped data in spark Dataframe

From Dev

Calculate random variables from grouped dataframe

From Dev

calculate sum of rows in pandas dataframe grouped by date

From Dev

A simpler way to calculate grouped percentages in a Spark dataframe?

From Dev

Dart How to calculate mean?

From Dev

How to calculate mean in python?

From Dev

how to calculate the mean with conditions?

From Dev

How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

From Dev

How to filter dataframe by grouped value?

From Dev

How to get rolling mean in grouped data?

From Dev

How to add a mean line for grouped data plots

From Dev

Calculate mean of specific rows Julia dataframe

From Dev

Calculate difference and mean over groups in DataFrame

Related Related

  1. 1

    How to calculate mean in a dataframe?

  2. 2

    Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

  3. 3

    how to calculate cumsum with depreciation in a grouped dataframe?

  4. 4

    How to calculate mean values grouped on another column in Pandas

  5. 5

    How to calculate grand mean and standard deviation for the grouped table?

  6. 6

    how to calculate mean values of the past n years grouped by two variables

  7. 7

    calculate mean grouped by col number

  8. 8

    How to calculate mean of specific rows in python dataframe?

  9. 9

    How to ignore the zero value to calculate the mean in the dataframe

  10. 10

    How to calculate RMS for dataframe column grouped by two column labels?

  11. 11

    Calculate medians of rows in a grouped dataframe

  12. 12

    How to calculate mean and standard deviation given a PySpark DataFrame?

  13. 13

    how to regroup, calculate the mean and generate new dataframe in R?

  14. 14

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

  15. 15

    How can I calculate the mean of a specific cell in a dataframe in Python?

  16. 16

    How to calculate difference on grouped df?

  17. 17

    Calculate quantile on grouped data in spark Dataframe

  18. 18

    Calculate random variables from grouped dataframe

  19. 19

    calculate sum of rows in pandas dataframe grouped by date

  20. 20

    A simpler way to calculate grouped percentages in a Spark dataframe?

  21. 21

    Dart How to calculate mean?

  22. 22

    How to calculate mean in python?

  23. 23

    how to calculate the mean with conditions?

  24. 24

    How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

  25. 25

    How to filter dataframe by grouped value?

  26. 26

    How to get rolling mean in grouped data?

  27. 27

    How to add a mean line for grouped data plots

  28. 28

    Calculate mean of specific rows Julia dataframe

  29. 29

    Calculate difference and mean over groups in DataFrame

HotTag

Archive