본문 바로가기
  • "You can't manage what you can't measure" Peter Drucker
데이터 분석 (with Rstudio)

Rstudio 데이터 분석 with FIFA 월드컵 데이터 #2

by Vitaminymc 2023. 8. 9.
반응형

국가별 본선 참가횟수

  • wcmatches.csv 또는 wcmatches_2020.csv (2022년 카타르 월드컵 추가) 데이터 세트는 월드컵 본선 결과를
    Home team과 Away tema으로 구분되어 결과가 정리됨
  • 같은(단일) 국가가 같은 월드컵 대회에서 Home team과 Away team으로 참여하는 것을 고려 필요
  • 즉, 참가 횟수는 Home team 실적과 Away team 실적을 구분하지 않고 횟수가 반영되어야 함

total_wcmatches  (wcmatches + wc2022, 2022년 대회까지 포함한 데이터 세트)

Rstudio with FIFA 월드컵 데이터 (2022년 카타르 추가) (tistory.com) 

 

Rstudio with FIFA 월드컵 데이터 (2022년 카타르 추가)

wcmatches_2020.csv : 2022년 Qatar (카타르) 월드컵 결과 추가 'wcmatches.csv' 데이터 세트는 1930년 우루과이 ~ 2018년 러시아 월드컵 결과 2022년 카타르 월드컵 결과 추가 반영 [Data Source] 아래 사이트 내용을

logistician.tistory.com

library(tidyverse)

# 대회(년도)의 Home team으로 경기를 치룬 데이터만 반환 
home_team_case <- total_wcmatches  %>% 
  group_by(year, home_team) %>%
  summarise(tmp_no = n_distinct(year)) 

# Home team 국가명을 team_nm이라는 새로운 변수에 저장
home_team_case$team_nm <- home_team_case$home_team

# Home team 국가명은 제외
home_team_case <- home_team_case %>% select(-home_team)


# 대회(년도)의 Away team으로 경기를 치룬 데이터만 반환
away_team_case <- total_wcmatches  %>%
  group_by(year, away_team) %>%
  summarise(tmp_no = n_distinct(year)) 

# Away team 국가명을 team_nm이라는 새로운 변수에 저장
away_team_case$team_nm <- away_team_case$away_team

# Away team 국가명은 제외
away_team_case <- away_team_case %>% select(-away_team)

# Home team 경기 데이터와 Away team 경기 데이터를 합침
both_case <- rbind(home_team_case, away_team_case)

# Home team, Away team 구분없이, team_nm 기준으로 중복없이 참가 횟수 Count
no_attemp <- both_case %>%
  group_by(team_nm) %>%
  summarise(
    att_no = n_distinct(year)  # n_distinct는 중복을 제거하고 Count
  ) %>%
  arrange (-att_no)
  team_nm att_no
1 Brazil 22
2 Argentina 18
3 Italy 18
4 Mexico 17
5 England 16
6 France 16
7 Spain 16
8 Belgium 14
9 Uruguay 14
10 Sweden 12
11 Switzerland 12
12 Netherlands 11
13 South Korea 11
14 United States 11
15 Germany 10
16 West Germany 10
17 Chile 9
18 Hungary 9
19 Poland 9
20 Cameroon 8
21 Czechoslovakia 8
22 Paraguay 8
23 Portugal 8
24 Scotland 8
25 Yugoslavia 8
26 Austria 7
27 Bulgaria 7
28 Japan 7
29 Romania 7
30 Soviet Union 7
31 Australia 6
32 Colombia 6
33 Costa Rica 6
34 Croatia 6
35 Denmark 6
36 Iran 6
37 Morocco 6
38 Nigeria 6
39 Saudi Arabia 6
40 Tunisia 6
41 Peru 5
42 Serbia 5
43 Algeria 4
44 Ecuador 4
45 Ghana 4
46 Russia 4
47 Bolivia 3
48 Egypt 3
49 Greece 3
50 Honduras 3
51 Ivory Coast 3
52 Northern Ireland 3
53 Norway 3
54 Republic of Ireland 3
55 Senegal 3
56 South Africa 3
57 Canada 2
58 El Salvador 2
59 New Zealand 2
60 North Korea 2
61 Slovenia 2
62 Turkey 2
63 Wales 2
64 Angola 1
65 Bosnia and Herzegovina 1
66 China PR 1
67 Cuba 1
68 Czech Republic 1
69 Dutch West Indies 1
70 East Germany 1
71 FR Yugoslavia 1
72 Haiti 1
73 Iceland 1
74 Iraq 1
75 Israel 1
76 Jamaica 1
77 Kuwait 1
78 Panama 1
79 Qatar 1
80 Slovakia 1
81 Togo 1
82 Trinidad and Tobago 1
83 Ukraine 1
84 United Arab Emirates 1
85 Zaire 1
  • Brazil이 총 22회로 가장 많이 참가함 (1930년 ~ 2022년 : 총 22번, 모든 대회 참가)
  • 대한민국은 총 11회 참가
  • Argentina가 18회로 두 번째로 보이나, Germany(10회, 통일 독일)와 West Germany(10회, 서독),
    East Germany(1회 동독) 참가 회수를 어떻게 분석할 것인지에 따라, 다른 결과 도출
    • 같은 나라로 보고, 참가 횟수를 재산정 할 것인지? 
    • 다른 나라로 분석할 것인지?
  • 독일과 유사한 경우로, Russia와 Soviet Union(소비에트연방, 소련) 등은 어떻게 할 것가?
    • Germany, West Germany, East Germany는 Germany로 통합하여 산출 필요
    • Czech, Czechoslovakia, Czech Republic는 Czech로 통합하여 산출 필요
    • Soviet Union, Russia는 Russia로 통합하여 산출 필요
    • Serbia, Yugoslavia, FR Yugoslavia는 Serbia로 통합하여 산출 필요
  • 곧 통일이 되어, South Korea, North Korea는 Korea로 통합하여 산출되길 바란다.

n_distinct() 함수 : 데이터의 중복성을 파악하거나 고유한 값을 계산할 때 사용

주어진 열(변수)에서 중복된 값을 제외하고 몇 개의 고유한 값이 있는지 계산하여 반환

n_distinct(..., na.rm = FALSE)

  • ...   : 고유한 값을 세고자 하는 열(변수)
  • na.rm  : 논리값으로, TRUE로 설정하면 결측값을 무시하고 고유한 값의 개수를 계산, 기본값은 FALSE

str(both_case)
gropd_df [862 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ year   : int [1:862] 1930 1930 1930 1930 1930 1930 1930 1930 1930 1934 ...
 $ tmp_no : int [1:862] 1 1 1 1 1 1 1 1 1 1 ...
 $ team_nm: chr [1:862] "Argentina" "Belgium" "Bolivia" "Brazil" ...
both_case$team_nm <- as.factor(both_case$team_nm)   # team_name을 factor 형태의 변수로 변경
str(both_case)
gropd_df [862 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ year   : int [1:862] 1930 1930 1930 1930 1930 1930 1930 1930 1930 1934 ...
 $ tmp_no : int [1:862] 1 1 1 1 1 1 1 1 1 1 ...
 $ team_nm: Factor w/ 85 levels "Algeria","Angola",..: 3 6 7 9 13 29 54 55 81 3 ...
no_attemp2 <- c%>%
  mutate (team_nm = fct_collapse(team_nm,
                                 Germany = c("Germany", "West Germany", "East Germany"),
                                 Czech = c("Czechoslovakia","Czech Republic"),
                                 Russia = c("Soviet Union", "Russia"),
                                 Serbia = c("Serbia", "Yugoslavia", "FR Yugoslavia")
    
  )) %>%
  group_by(team_nm) %>%
  summarise(
    att_no = n_distinct(year)
  ) %>%
  arrange (-att_no)
  team_nm att_no
1 Brazil 22
2 Germany 20
3 Argentina 18
4 Italy 18
5 Mexico 17
6 France 16
7 Spain 16
8 England 16
9 Belgium 14
10 Uruguay 14
11 Serbia 13
12 Sweden 12
13 Switzerland 12
14 United States 11
15 Netherlands 11
16 South Korea 11
17 Russia 11
18 Chile 9
19 Czech 9
20 Hungary 9
21 Poland 9
22 Paraguay 8
23 Scotland 8
24 Portugal 8
25 Cameroon 8
26 Romania 7
27 Austria 7
28 Bulgaria 7
29 Japan 7
30 Colombia 6
31 Morocco 6
32 Australia 6
33 Iran 6
34 Tunisia 6
35 Denmark 6
36 Costa Rica 6
37 Nigeria 6
38 Saudi Arabia 6
39 Croatia 6
40 Peru 5
41 Algeria 4
42 Ecuador 4
43 Ghana 4
44 Bolivia 3
45 Egypt 3
46 Norway 3
47 Northern Ireland 3
48 Honduras 3
49 Republic of Ireland 3
50 Greece 3
51 South Africa 3
52 Senegal 3
53 Ivory Coast 3
54 Turkey 2
55 Wales 2
56 North Korea 2
57 El Salvador 2
58 New Zealand 2
59 Canada 2
60 Slovenia 2
61 Cuba 1
62 Dutch West Indies 1
63 Israel 1
64 Haiti 1
65 Zaire 1
66 Kuwait 1
67 Iraq 1
68 United Arab Emirates 1
69 Jamaica 1
70 China PR 1
71 Angola 1
72 Togo 1
73 Trinidad and Tobago 1
74 Ukraine 1
75 Slovakia 1
76 Bosnia and Herzegovina 1
77 Iceland 1
78 Panama 1
79 Qatar 1
  • Germany : 20회 (Germany 10, West Germany 10, East Germany 1)
  • 1974년 동독이 지역예선 통과를 하여 월드컵 출전, 동서독이 모두 참가한 유일한 월드컵
    이후 동독은 통일될 때까지 월드컵에 나가지 못 함 
    https://namu.wiki/w/1974%20FIFA%20%EC%9B%94%EB%93%9C%EC%BB%B5%20%EC%84%9C%EB%8F%85
  • Serbia :  13회 (Yugoslavia 8, Serbia 5)
    • wcmatches.csv 데이터 세트의 1998년 프랑스 대회의 오류 (Yugoslavia와 Serbia 혼재, 아래 표 참조)
  • Russia : 11회 (Soviet Union 7, Russia 4)
  • Czech :  9회 (Czechoslovakia 8, Czech Republic 1)

fct_collapse() 함수 : factor의 카테고리를 병합하거나 재구성할 때 사용

fct_collapse(.f, ...)

  • .f  : 통합하거나 재구성하려는 요인(factor)
  • ...  :  통합될 카테고리들의 목록을 나열하며, 새로운 카테고리 이름과
           해당 카테고리를 구성하는 기존 카테고리들의 리스트를 입력

  mutate (team_nm = fct_collapse(team_nm,
                                 "Germany" = c("West Germany", "East Germany"),
                                 "Czech" = c("Czechoslovakia","Czech Republic"),
                                 "Russia" = c("Soviet Union"),
                                 "Serbia" = c("Yugoslavia", "FR Yugoslavia") 
                                )

 


Serbia 참가 년도 확인

both_case %>%
  filter (team_nm %in% c("Serbia", "FR Yugoslavia","Yugoslavia")) %>%
  arrange(year) %>%
  View()
  year tmp_no team_nm
1 1930 1 Yugoslavia
2 1950 1 Yugoslavia
3 1954 1 Yugoslavia
4 1958 1 Yugoslavia
5 1962 1 Yugoslavia
6 1974 1 Yugoslavia
7 1974 1 Yugoslavia
8 1982 1 Yugoslavia
9 1982 1 Yugoslavia
10 1990 1 Yugoslavia
11 1990 1 Yugoslavia
12 1998 1 FR Yugoslavia
13 1998 1 FR Yugoslavia
14 1998 1 Serbia
15 2006 1 Serbia
16 2006 1 Serbia
17 2010 1 Serbia
18 2010 1 Serbia
19 2018 1 Serbia
20 2018 1 Serbia
21 2022 1 Serbia
22 2022 1 Serbia
total_wcmatches %>%
  filter (year == 1998) %>%
  filter (home_team %in% c("Serbia", "FR Yugoslavia") | away_team %in% c("Serbia", "FR Yugoslavia")) %>%
    arrange(year) %>%
  View()
stage home_team away_team home_score away_score outcome win_conditions winning_team losing_team
Group F FR Yugoslavia Iran 1 0 H   FR Yugoslavia Iran
Group F Germany FR Yugoslavia 2 2 D      
Group F United States Serbia 0 1 A   Serbia United States
Round of 16 Netherlands FR Yugoslavia 2 1 H   Netherlands FR Yugoslavia

Serbia 축구팀 역사를 보면 1998년은 FR Yugoslavia가 맞음

  • Serbia within Yugoslavia (1920–1992)
  • FR Yugoslavia/Serbia & Montenegro era (1992–2006)
  • Independent Serbia (2006–present)

Source : https://en.wikipedia.org/wiki/Serbia_national_football_team

 

Serbia national football team - Wikipedia

From Wikipedia, the free encyclopedia Men's national association football team representing Serbia SerbiaNickname(s)Орлови / Оrlovi (Eagles)AssociationFudbalski savez Srbije (FSS)ConfederationUEFA (Europe)Head coachDragan StojkovićCaptainDušan Tad

en.wikipedia.org

Image Source :&nbsp;https://namu.wiki/w/1998%20FIFA%20%EC%9B%94%EB%93%9C%EC%BB%B5%20%ED%94%84%EB%9E%91%EC%8A%A4

728x90