[英]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. 我曾尝试使用max和count但没有成功。 Dismissal_kind is a character variable here.
Dismissal_kind是一个字符变量。
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, 您可以在语句
dismissal_kind %in% c("stumped", "bowled", "lbw","caught")
对TRUE
出现进行求和,因此,
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.