简体   繁体   中英

How to group_by multiple column /variable

Here is my code:

  Groupby_sample %>%
  group_by(Network, Merchant, Status) %>%
  summarise(Tranx_count = n())

I want something where each observation is grouped as one like the excel pivot table I have shown below. Python gives something equivalent, but R is grouping per 1 observation.

我想要这样的东西

Here is a sample dataset:

Merchant    Recipient   Network Type    FaceValue   Date    Status
Economy 7012086632  Newest  Airtime 100 02/04/2021 0:05 Transaction Declined
Economy 9013347171  Newest  Airtime 100 02/04/2021 0:06 Transaction Declined
Economy 7083816093  Newest  Airtime 200 02/04/2021 0:08 Transaction Declined
polly   8126029470  Newest  Airtime 2000    02/04/2021 0:09 Transaction Declined
Star    8020391914  Newest  Airtime 200 02/04/2021 0:10 DECLINED
Munifat 7012349167  Newest  Airtime 100 02/04/2021 0:12 DECLINED
Munifat 9078126934  AT AT   Airtime 500 02/04/2021 0:13 DECLINED
polly   9070149314  AT AT   Airtime 100 02/04/2021 0:17 DECLINED
polly   9012964375  AT AT   Airtime 500 02/04/2021 0:18 DECLINED
polly digital   9026410183  AT AT   Airtime 1000    02/04/2021 0:19 DECLINED
Economy 7088794494  AT AT   Airtime 500 02/04/2021 0:23 Transaction Declined
Economy 7082168900  AT AT   Airtime 100 02/04/2021 0:33 Transaction Declined
Economy 9020689920  AT AT   Airtime 100 02/04/2021 3:43 Transaction Declined
polly digital   9049041083  AT AT   Airtime 100 02/04/2021 4:07 FAILED
Star    9019433081  Newest  Airtime 1000    02/04/2021 4:09 FAILED

please note that my r knowledge is limited

Try, with {tidyverse},

library(tidyverse)
DF <- 
  structure(list(
    Merchant = c("Economy", "Economy", "Economy", "polly", "Star", "Munifat", "Munifat", "polly", "polly", "polly digital", "Economy", "Economy", "Economy", "polly digital", "Star"), Recipient = c("7012086632", "9013347171", "7083816093", "8126029470", "8020391914", "7012349167", "9078126934", "9070149314", "9012964375", "9026410183", "7088794494", "7082168900", "9020689920", "9049041083", "9019433081"), 
    Network = c("Newest", "Newest", "Newest", "Newest", "Newest", "Newest", "AT AT", "AT AT", "AT AT", "AT AT", "AT AT", "AT AT", "AT AT", "AT AT", "Newest"), 
    Type = c("Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime", "Airtime"), FaceValue = c(100L, 100L, 200L, 2000L, 200L, 100L, 500L, 100L, 500L, 1000L, 500L, 100L, 100L, 100L, 1000L), 
    Date = structure(c(1617321900, 1617321960, 1617322080, 1617322140, 1617322200, 1617322320, 1617322380, 1617322620, 1617322680, 1617322740, 1617322980, 1617323580, 1617334980, 1617336420, 1617336540), tzone = "UTC", class = c("POSIXct", "POSIXt")),     
    Status = c("Transaction Declined", "Transaction Declined",     "Transaction Declined", "Transaction Declined", "DECLINED",     "DECLINED", "DECLINED", "DECLINED", "DECLINED", "DECLINED",     "Transaction Declined", "Transaction Declined", "Transaction Declined",     "FAILED", "FAILED")), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), 
    row.names = c(NA, -15L))

DF %>% count(Network, Status, Merchant)
# A tibble: 10 x 4
   Network Status               Merchant          n
   <chr>   <chr>                <chr>         <int>
 1 AT AT   DECLINED             Munifat           1
 2 AT AT   DECLINED             polly             2
 3 AT AT   DECLINED             polly digital     1
 4 AT AT   FAILED               polly digital     1
 5 AT AT   Transaction Declined Economy           3
 6 Newest  DECLINED             Munifat           1
 7 Newest  DECLINED             Star              1
 8 Newest  FAILED               Star              1
 9 Newest  Transaction Declined Economy           3
10 Newest  Transaction Declined polly             1

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