简体   繁体   中英

Subset rows based on values of a column

I need to subset rows based on filtered values of a column and group by another column.

    Bowler               dismissal_kind      
    F du Plessis          stumped   
    MJ McClenaghan        run out   
    F du Plessis          bowled
    HH pandya              lbw
    HH pandya             bowled
    F du Plessis          caught
    F du Plessis          run out
    JJ Bumrah             caught 
    DL Chahar

I have tried to use max and count but did not work out. Dismissal_kind is a character variable here.

innings%>%
summarise(wickets =  max(count(dismissal_kind %in% c("stumped", 
"bowled", "lbw","caught"))))%>%
group_by(bowler)%>%
arrange(desc(wickets))%>%
top_n(10)

I want to group by bowler and count only filtered rows. I want something like

bowler              dismissal_kind
F du Plessis         3
HH pandya            2
JJ Bumrah            1

How can i achieve this result. I am not able to sum this character variable. Is there any workaround to achieve this expected result.

You can sum the occurences of TRUE in your statement dismissal_kind %in% c("stumped", "bowled", "lbw","caught") , hence,

tb %>% 
  group_by(Bowler) %>% 
  summarise(Count_Wickets = sum(dismissal_kind %in% c("stumped", 
                                      "bowled", "lbw","caught"))) %>% 
  arrange(desc(Count_Wickets))

# A tibble: 5 x 2
  Bowler         Count_Wickets
  <chr>                  <int>
1 F du Plessis               3
2 HH pandya                  2
3 JJ Bumrah                  1
4 DL Chahar                  0
5 MJ McClenaghan             0

Data:

tibble::tribble(
~Bowler, ~dismissal_kind,      
"F du Plessis", "stumped",   
"MJ McClenaghan", "run out",   
"F du Plessis", "bowled",
"HH pandya", "lbw",
"HH pandya", "bowled",
"F du Plessis", "caught",
"F du Plessis", "run out",
"JJ Bumrah", "caught",
"DL Chahar", NA    
) -> tb

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