Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate mean by group with dplyr [closed]

Tags:

r

dplyr

I'm practicing dplyr package, and I have some problem when calculating mean by group. Here's a subset of my data

head(Data)
CodeProject Price
Pr1           3
Pr1           4
Pr1           5
Pr2           6
Pr2           9

I would like to calculate mean for each project, I tried the following code

library(dplyr)
Data  %>%
group_by(Data$CodeProject) %>%
summarize(
n = n(),
mean_pr = mean(Price, na.rm=T)
)

But when I do that, I get this result :

Data$CodeProject  n   mean_pr
Pr1               3     5.4
Pr2               2     5.4

I tried to add dplyr::summarize, but same result

How can I fix that ?

Thank you very much

like image 524
Layale Avatar asked Nov 04 '25 05:11

Layale


1 Answers

You were almost there:

Data  %>%
    group_by(CodeProject) %>%
    summarise(
        n = n(),
        mean_pr = mean(Price, na.rm=T))
## A tibble: 2 x 3
#  CodeProject     n mean_pr
#  <fct>       <int>   <dbl>
#1 Pr1             3    4.00
#2 Pr2             2    7.50
like image 144
Maurits Evers Avatar answered Nov 06 '25 02:11

Maurits Evers