在 R 中进行分组时是否可以计算分布

Is it possible to calculate a distribution while doing a group by in R

我知道在做 group_by 时我可以 summarise 并计算频率、总和、平均值、中位数、标准差等。我想知道在总结时我是否可以计算概率分布。例如

dat%>%group_by(A, B)%>%summarise(C_dist = density(C))

我试过在 r 中这样做。但是我收到以下错误。

Error in summarise_impl(.data, dots) : 
  Evaluation error: need at least 2 points to select a bandwidth automatically.

列中没有任何缺失值。

我宁愿使用 tapply()

tryCatch 位确保当一个组只有一个成员时,返回 NA 而不是让整个事情停止。

set.seed(1)
n <- 20
dtf <- data.frame(d=runif(n), 
                 g1=sample(1:3, n, replace=TRUE), 
                 g2=sample(c("A", "B"), n, replace=TRUE))

agg <- with(dtf, 
         tapply(d, list(g1, g2), 
         FUN=function(x) {
             tryCatch(density(x), error=function(e) NA)
         }))

str(agg)
agg[["2", "A"]]
# Call:
#    density.default(x = x)

# Data: x (3 obs.); Bandwidth 'bw' = 0.1733

#        x                 y           
#  Min.   :-0.2543   Min.   :0.008613  
#  1st Qu.: 0.1663   1st Qu.:0.156751  
#  Median : 0.5869   Median :0.699978  
#  Mean   : 0.5869   Mean   :0.593340  
#  3rd Qu.: 1.0074   3rd Qu.:0.902087  
#  Max.   : 1.4280   Max.   :1.199607