简体   繁体   中英

group_by() and summarise() keeping the values without grouping

I want to summarise values of two created groups and keep the values of the total sample.

What I have so far:

data <- structure(list(big_four = c(0L, 0L, 0L, 1L, 1L, 0L), idade_em_2022 = c(46L,38L, 40L, 23L, 27L, 27L), total_de_cooperados = c(8665L, 2198L, 2338L, 3017L, 19608L, 27515L), numeroAgencias = c(8L, 5L, 0L, 4L, 19L, 29L)), row.names = c(NA, 6L), class = "data.frame")

data |>
  dplyr::group_by(big_four) |>
  dplyr::filter(
    !is.na(idade_em_2022 & total_de_cooperados)
  ) |>
  dplyr::summarise_at(
    dplyr::vars(idade_em_2022, total_de_cooperados, numeroAgencias),
    list(
      minimo = min,
      media = mean,
      maximo = max,
      desvio = sd
    )
  ) |> 
  t() |>
  as.data.frame(, -1) |>
  dplyr::arrange() |>
  dplyr::rename(n_BigFour = 1, BigFour = 2) |>
  tibble::rownames_to_column("variavel") |>
  tidyr::extract("variavel", into = c("caracteristica", "condicao"), "(.*)_([^_]+)$") |>
  dplyr::arrange(caracteristica) |>
  print()
#>         caracteristica condicao    n_BigFour      BigFour
#> 1                  big     four     0.000000     1.000000
#> 2        idade_em_2022   minimo    27.000000    23.000000
#> 3        idade_em_2022    media    37.750000    25.000000
#> 4        idade_em_2022   maximo    46.000000    27.000000
#> 5        idade_em_2022   desvio     7.932003     2.828427
#> 6       numeroAgencias   minimo     0.000000     4.000000
#> 7       numeroAgencias    media    10.500000    11.500000
#> 8       numeroAgencias   maximo    29.000000    19.000000
#> 9       numeroAgencias   desvio    12.767145    10.606602
#> 10 total_de_cooperados   minimo  2198.000000  3017.000000
#> 11 total_de_cooperados    media 10179.000000 11312.500000
#> 12 total_de_cooperados   maximo 27515.000000 19608.000000
#> 13 total_de_cooperados   desvio 11944.409208 11731.608607

Created on 2022-06-22 by the reprex package (v2.0.1)

I want to add a column Total with all values of the sample (big_four 0 and 1 together)

Like this:

#>         caracteristica condicao    n_BigFour      BigFour Total
#> 1                  big     four     0.000000     1.000000  0 and 1
#> 2        idade_em_2022   minimo    27.000000    23.000000   23
#> 3        idade_em_2022    media    37.750000    25.000000   28
#> 4        idade_em_2022   maximo    46.000000    27.000000   46
#> 5        idade_em_2022   desvio     7.932003     2.828427   9.9
#> 6       numeroAgencias   minimo     0.000000     4.000000   0
#> 7       numeroAgencias    media    10.500000    11.500000   10.8
#> 8       numeroAgencias   maximo    29.000000    19.000000   29
#> 9       numeroAgencias   desvio    12.767145    10.606602   10.9
#> 10 total_de_cooperados   minimo  2198.000000  3017.000000   2198
#> 11 total_de_cooperados    media 10179.000000 11312.500000   10556
#> 12 total_de_cooperados   maximo 27515.000000 19608.000000   27515
#> 13 total_de_cooperados   desvio 11944.409208 11731.608607   10652

Can I do it in one pipeline ?

You may try

data |>
  mutate(big_four = paste0(c(unique(big_four)), collapse = " and ")) |>
  rbind(data) |>
  dplyr::group_by(big_four) |>
  dplyr::filter(
    !is.na(idade_em_2022 & total_de_cooperados)
  ) |>
  dplyr::summarise_at(
    dplyr::vars(idade_em_2022, total_de_cooperados, numeroAgencias),
    list(
      minimo = min,
      media = mean,
      maximo = max,
      desvio = sd
    )
  ) |> 
  t() |>
  as.data.frame(, -1) |>
  dplyr::arrange() |>
  dplyr::rename(n_BigFour = 1, BigFour = 3, total = 2) |>
  tibble::rownames_to_column("variavel") |>
  tidyr::extract("variavel", into = c("caracteristica", "condicao"), "(.*)_([^_]+)$") |>
  dplyr::arrange(caracteristica) %>%
  relocate(total, .after = BigFour)

        caracteristica condicao n_BigFour  BigFour    total
1                  big     four         0        1  0 and 1
2        idade_em_2022   minimo        27       23       23
3        idade_em_2022    media     37.75    25.00    33.50
4        idade_em_2022   maximo        46       27       46
5        idade_em_2022   desvio  7.932003 2.828427 9.093954
6       numeroAgencias   minimo         0        4        0
7       numeroAgencias    media  10.50000 11.50000 10.83333
8       numeroAgencias   maximo        29       19       29
9       numeroAgencias   desvio  12.76715 10.60660 10.98029
10 total_de_cooperados   minimo      2198     3017     2198
11 total_de_cooperados    media  10179.00 11312.50 10556.83
12 total_de_cooperados   maximo     27515    19608    27515
13 total_de_cooperados   desvio  11944.41 11731.61 10652.23

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM