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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With