rfrequencyfrequency-distribution

Frequency Table with Column of Average in R


Given a dataframe df which I generate as follows:

set.seed(1)
b <- runif(100)
set.seed(1)
a <- sample.int(9, 100, replace = TRUE)
df <- data.frame(a,b)

I formed a frequency table as shown below but not satisfied:

sortted_a <- data.frame(table(df$a))
sortted_a
#  Var1 Freq
#1    1   14
#2    2    8
#3    3   10
#4    4    9
#5    5   11
#6    6   11
#7    7    5
$8    8   14
#9    9   18

I rather want a table that will not only show the frequency of vector a but will display frequency of a along with the associated averages of vector b as follows in R:

#  Var1 Freq   Ave_b
#1    1   14   0.6750
#2    2    8   0.0027
#3    3   10   0.8298
#4    4    9   0.1873
#5    5   11   0.3874
#6    6   11   0.7632
#7    7    5   0.5812
$8    8   14   0.5478
#9    9   18   0.4389

Solution

  • If you want to stick with base R, you can use tapply() to find the average of b by a and then cbind() with your sorted data.frame:

    ave_b <- tapply(df$b,df$a,mean)
    new_df <- cbind(sortted_a, ave_b)