如何模拟R世界杯比赛

时间:2017-11-17 19:50:34

标签: r algorithm simulation

因此明年世界杯的资格赛已经结束,抽签将在几周内完成。

32支队伍被投入" pot"根据他们的世界排名,然后分组,以便每个组包含来自每个底池的团队。很简单。

    library(tidyverse)

    seeds <- read_csv("https://gist.github.com/eldenvo/fb0d27fcb5d322b538d06cdc06b1c559/raw/3eaaab91c29d2dde6c9a28377b325844b834df13/seeds.csv")

    head(seeds)

    # A tibble: 6 x 4
       nation  rank   pot        confed
        <chr> <int> <int>         <chr>
    1  Russia    65     1        Europe
    2  Brazil     2     1 South America
    3    Iran    34     3          Asia
    4   Japan    44     4          Asia
    5  Mexico    16     2 North America
    6 Belgium     5     1        Europe

groups <- letters[c(1:8)]

draw <- seeds %>% filter(pot == 1) %>% sample_n(8) %>% mutate(group = groups)

for (i in 2:4){
  df <- seeds %>% filter(pot == i) %>% sample_n(8) %>% mutate(group = groups)

  draw <- bind_rows(draw, df)
}

print(draw)

# A tibble: 32 x 5
      nation  rank   pot        confed group
       <chr> <int> <int>         <chr> <chr>
 1    Russia    65     1        Europe     a
 2 Argentina     4     1 South America     b
 3    Brazil     2     1 South America     c
 4   Belgium     5     1        Europe     d
 5  Portugal     3     1        Europe     e
 6    Poland     6     1        Europe     f
 7   Germany     1     1        Europe     g
 8    France     7     1        Europe     h
 9   England    12     2        Europe     a
10   Uruguay    17     2 South America     b
# ... with 22 more rows

根据他们的底池,我们让32支球队中的每支球队随机分配一组A到H.

但是,还有一个限制因素:同一个联盟中不可能有两个团队被吸引到同一个团体中(除了欧洲,你不能超过两个团队)。

这意味着在上面的例子中,在现实生活中,在第二轮选秀中,乌拉圭被吸引到B组,但阿根廷已经在那里,并且不能有两支南美球队在同一场比赛中组。然后将乌拉圭改为C组,并为B组选出一支新队伍。依此类推。

那么您如何将这些约束条件放入R?

的模拟中呢?

0 个答案:

没有答案