how can get summary rows

I have a data frame that want to get summary from row and group by (id) data set and summarise them , but give me mean column v for any row and not sum cloumn v and divide any row.

data.frame(id = c("a", "b", "c"), value = c(25, 35, 45, 15, 20, 55)) %>% 
    group_by(id) %>% 
    summarise(v = n(), m = mean(v)) 

id        v     m
  <fct> <int> <dbl>
1 a         2     2
2 b         2     2
3 c         2     2

i wnat this type of mean cloumn

id        v     m
  <fct> <int> <dbl>
1 a         2     0.3333333
2 b         2     0.3333333
3 c         2     0.3333333

Is this what you want?

library(dplyr, warn.conflicts = FALSE)

data.frame(id = c("a", "b", "c"), value = c(25, 35, 45, 15, 20, 55)) %>% 
  group_by(id) %>% 
  summarise(v = n()) %>% 
  mutate(m = v / sum(v))
#> # A tibble: 3 x 3
#>   id        v     m
#>   <chr> <int> <dbl>
#> 1 a         2 0.333
#> 2 b         2 0.333
#> 3 c         2 0.333

Created on 2020-05-06 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.