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
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.
Comments