简体   繁体   English

使用DPLYR在R中组合多个操作

[英]Combined multiple operations in R using DPLYR

I'm trying to use DPLYR to retrieve and summarize data. 我正在尝试使用DPLYR检索和汇总数据。 I wrote the below and it works, but I would like to combine this all into one statement. 我写了下面的代码,它可以工作,但是我想将所有这些结合成一个语句。 Is this possible? 这可能吗?

create datasets 创建数据集

set.seed(1)
dbo_games <- data.frame(
  name = sample(c("Team1","Team2","Team3","Team4","Team5","Team6","Team7","Team8","Team9","Team10")),
  total_games = sample(1:10)

)

set.seed(1)
dbo_wins <- data.frame(
  name = sample(c("Team1","Team2","Team3","Team4","Team5","Team6","Team7","Team8","Team9","Team10")),
  tota_wins = sample(c("yes", "no"), 10, replace = TRUE)
)
total_games <- con %>% tbl("dbo_games")
total_wins <- con %>% tbl("dbo_wins")

total<- total_games %>% filter(games > 12) %>%
  group_by(NAME) %>%
  summarise(total_games = n_distinct(game_id)) %>% collect()

wins <- total_wins %>% filter( win == 'Y') %>%
  group_by(NAME) %>%
  summarise(total_wins = n_distinct(game_id)) %>% collect()

perc_win <- total %>% left_join(wins) %>%
  mutate(pct_won = total_wins/total_games)

This code works, but I believe there is likely a more succinct way of writing the code to achieve the same results. 这段代码有效,但是我相信可能会有更简洁的方式编写代码来达到相同的结果。 Any thoughts? 有什么想法吗?

It would have been easier to address this if you had shared sample data and why you are doing what you are doing. 如果您共享了示例数据以及执行操作的原因,则解决该问题会更容易。

However, you could still chain them together as below: 但是,您仍然可以将它们链接在一起,如下所示:

total_games %>%
  filter(games > 12) %>%
  group_by(NAME) %>%
  summarise(total_games = n_distinct(game_id)) %>%
  left_join(total_wins %>% filter( win == 'Y') %>%
              group_by(NAME) %>%
              summarise(total_wins = n_distinct(game_id))) %>%
  mutate(pct_won = total_wins/total_games)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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