And a data.table version for good measure.
library(data.table)
df <- as.data.table(df)
Once you have the data.table, it's then a simple operation:
df[,count := .N,by="gender"]
df
# gender age count
#1: m 18 4
#2: f 14 2
#3: m 18 4
#4: m 18 4
#5: m 15 4
#6: f 15 2