我有以下数据:
df <- tibble(
"result" = c("win", "loss", "win", "win", "loss", "win", "win", "loss"),
"team" = c("TBL", "NJD", "TBL", "STL", "TBL", "STL", "NJD", "STL"),
)
该表有两列-result
和team
。我想计算一个团队获得“胜利”的百分比。我使用dplyr
快速计算了每个结果(“赢”或“输”)-
df_win <- df %>%
group_by(result, team) %>%
filter(result == "win") %>%
summarise(count = n())
df_loss <- df %>%
group_by(result, team) %>%
filter(result == "loss") %>%
summarise(count = n())
我想使用mutate
创建一个公式来计算球队获得“获胜”的次数(反之则代表“损失”):
df %>%
mutate(team_win_perc = ((df_win / df_loss) * 0.1 )))
但是很明显,一个人不能只将两个表相乘而不会产生任何后果:
Error in mutate_impl(.data, dots) :
Evaluation error: non-numeric argument to binary operator.
问我有时候是白痴,我该如何格式化?
答案 0 :(得分:1)
这会产生您想要的东西吗?
df %>%
group_by(team) %>%
summarise(win_pct = sum(result == "win") / n())