dt <-
  data.frame(
    id = 182,
    group = c(rep("A", 20), rep("B", 32), rep("C", 50), rep("D", 80)),
    y = rnorm(182)
  )

# 採用 aggregate
set.seed(1234)
aggregate(dt$y, list(dt$group), function(x){
  N <- length(x)
  n <- c(15, 20, 25, 35)[which(c(20, 32, 50, 80) %in% N)] # 判斷要抽幾個
  ind <- sample(1:N, n)
  mean(x[ind])
})
#   Group.1          x
# 1       A -0.2242983
# 2       B  0.3519958
# 3       C  0.1363614
# 4       D  0.4146774

# 採用 tapply
set.seed(1234)
tapply(dt$y, dt$group, function(x){
  N <- length(x)
  n <- c(15, 20, 25, 35)[which(c(20, 32, 50, 80) %in% N)] # 判斷要抽幾個
  mean(x[sample(1:N, n)])
})
#          A          B          C          D 
# -0.2242983  0.3519958  0.1363614  0.4146774 
