我正在try 创建一个输出,该输出按因素级别计算计数占总计数的百分比(在数据框中),但似乎不知道如何在输出中保留分组 struct .
我可以得到我想除以的总数.
df %>% summarise(sum(num))
# 15
...和按组计算的总数...
df %>% group_by(species) %>% summarise(sum(num))
# A tibble: 3 × 2
# species `sum(num)`
# <chr> <int>
# 1 Farfantepenaeus duorarum 4
# 2 Farfantepenaeus notialis 0
# 3 Farfantepenaeus spp 11
但我不能让它变成这个样子.
# ???
# species Percent
# <chr> <int>
# 1 Farfantepenaeus duorarum 4 / 15 = 0.267
# 2 Farfantepenaeus notialis 0 / 15 = 0.000
# 3 Farfantepenaeus spp 11 / 15 = 0.733
我得到的最接近的结果是这样,但因为我使用了reFrame(),所以它返回未分组的数据
df %>% group_by(species) %>%
summarise(factor_count=sum(num)) %>%
# ungroup() %>%
# Wanring: # Please use `reframe()` instead., When switching from `summarise()`
# to `reframe()`, remember that `reframe()` always returns an ungrouped data
reframe(percent=factor_count/sum(df$num))
# A tibble: 3 × 1
percent
<dbl>
1 0.267
2 0
3 0.733
生效日期:
> dput(df)
structure(list(species = c("Farfantepenaeus notialis", "Farfantepenaeus spp",
"Farfantepenaeus notialis", "Farfantepenaeus notialis", "Farfantepenaeus duorarum",
"Farfantepenaeus duorarum", "Farfantepenaeus notialis", "Farfantepenaeus spp",
"Farfantepenaeus duorarum", "Farfantepenaeus spp", "Farfantepenaeus notialis",
"Farfantepenaeus duorarum", "Farfantepenaeus spp", "Farfantepenaeus notialis",
"Farfantepenaeus notialis", "Farfantepenaeus spp", "Farfantepenaeus duorarum",
"Farfantepenaeus spp", "Farfantepenaeus spp", "Farfantepenaeus duorarum",
"Farfantepenaeus duorarum", "Farfantepenaeus spp", "Farfantepenaeus spp",
"Farfantepenaeus spp", "Farfantepenaeus notialis"), num = c(0L,
0L, 0L, 0L, 1L, 0L, 0L, 2L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 3L, 0L, 2L, 4L, 0L)), row.names = c(159897L, 174698L,
236857L, 190237L, 327321L, 272931L, 304567L, 75538L, 109206L,
351373L, 280332L, 163966L, 282183L, 341197L, 316962L, 354703L,
343971L, 95333L, 244258L, 254061L, 87561L, 186908L, 221318L,
258688L, 97737L), class = "data.frame")