0

From a data frame like this

data.frame(id= c(1,1,2,3,3), visits = c(32,30,1,2,4))

How is it possible to produce a average number column of visits per numi but also add a new column which will have the number/frequency of numbers exist in numi column Expected output:

data.frame(id=  c(1,2,3), numi_frq = c(2,1,2), average = c(31,1,3))
Nathalie
  • 1,228
  • 7
  • 20
  • 1
    Try `aggregate(. ~ id, d1, FUN = function(i) c(numi_fq = length(i), average = mean(i)))` – Sotos Jul 06 '20 at 13:58

1 Answers1

2
library(dplyr)
Data <- data.frame(id= c(1,1,2,3,3), visits = c(32,30,1,2,4))
Data %>% group_by(id)%>% summarise(N=n(),Avg=mean(visits))

# A tibble: 3 x 3
     id     N   Avg
  <dbl> <int> <dbl>
1     1     2    31
2     2     1     1
3     3     2     3
Duck
  • 39,058
  • 13
  • 42
  • 84