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

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

by Vitaminymc 2023. 8. 6.
반응형

Data source  : 월드컵 경기 결과 (1930년 우루과이 ~  2018년 러시아)

https://github.com/rfordatascience/tidytuesday/tree/master/data/2022/2022-11-29

https://github.com/rfordatascience/tidytuesday/blob/master/data/2022/2022-11-29/wcmatches.csv

wcmatches <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-11-29/wcmatches.csv')
  • wcmatches 데이터 세트에는 1930년 우루과이부터 2018년 러시아까지 월드컵 경기 결과를 다룸
  • 데이터 세트에는 경기가 진행된 도시, 승리한 팀 또는 경기 결과 , 득점, 무승부를 기록한 결과
  • win_condition은 승리한 쪽이 게임에서 연장 시간 Goal, 승부 차기 발생 여부 등을 보여 줌
    • 예) 2002년 한일 월드컵

Korea Japan 2002

Image source : https://en.wikipedia.org/wiki/2002_FIFA_World_Cup

city stage home
_team
away
_team
home
_score
away
_score
out_
come
win_conditions winning_team losing
_team
date
Daejeon Round of 16 South Korea Italy 2 1 H South Korea won in AET South Korea Italy 2002-
06-18
Gwangju Quarter
finals
South Korea Spain 0 0 H South Korea won in penalties (5 - 3) South Korea Spain 2002-
06-22

AET : after extra time
(축구 경기 결과를 제공할 때 허용된 일반적인 시간이 끝난 후에도 경기가 계속되었음을 보여주기 위해 사용됨)

월드컵 공동 개최로 합의했을 당시, 결승전은 일본에서 하고,
공식 대회 명칭은 Korea-Japan 월드컵으로 정했다고 들은 것 같다.
아래 데이터를 보면, 2002년 데이터의 country는 South Korea와 Japan으로 구분되어 있다.
2002 FIFA World Cup이 더 중요한 의미이지, 한일 (Korea-Japan) World Cup은 별의미가 없는 것 같다.
다른 나라 사람들에게는 브라질과 독일이 결승전을 한, 일본과 Yokohama가 더 기억에 남아있지 않을까?
당시 대한민국 축구협회는 어떤 생각으로 합의했을까? 내용보다는 명분이나 형식만 중요하게 여긴 것은 아니였겠지.

wcmatches.csv

variable class description
year double year  (월드컵 대회 년도)
country character country  (월드컵 개최국)
city character city (경기 도시)
stage character stage (예선, 16강, 8강, 준결승, 결승)
home_team character home_team (홈팀)
away_team character away_team (어웨이팀)
home_score double home_score  (홈팀 점수)
away_score double away_score (어웨이팀 점수)
outcome character outcome (승림팀 : H, A  무승부 : D)
win_conditions character win_conditions  (승리 조건)
winning_team character winning_team (승리 국가명)
losing_team character losing_team (패배 국가명)
date double date (경기 일자)
month character month  (경기 월)
dayofweek character dayofweek (경기 요일)

데이터 불러오기 (다운로드하여, PC에 저장한 경우)

getwd()  # 현재 working directory 확인, 파일을 저장한 곳과 Working Dierectory가 동일한지 확인

Working Directory 설정

wcmatches <- read.csv('wcmatches.csv')

str(wcmatches) #데이터 세트 전체 구성 보기
'data.frame': 900 obs. of  15 variables:
 $ year          : int  1930 1930 1930 1930 1930 1930 1930 1930 1930 1930 ...
 $ country       : chr  "Uruguay" "Uruguay" "Uruguay" "Uruguay" ...
 $ city          : chr  "Montevideo" "Montevideo" "Montevideo" "Montevideo" ...
 $ stage         : chr  "Group 1" "Group 4" "Group 2" "Group 3" ...
 $ home_team     : chr  "France" "Belgium" "Brazil" "Peru" ...
 $ away_team     : chr  "Mexico" "United States" "Yugoslavia" "Romania" ...
 $ home_score    : int  4 0 1 1 1 3 0 0 1 6 ...
 $ away_score    : int  1 3 2 3 0 0 4 3 0 3 ...
 $ outcome       : chr  "H" "A" "A" "A" ...
 $ win_conditions: chr  "" "" "" "" ...
 $ winning_team  : chr  "France" "United States" "Yugoslavia" "Romania" ...
 $ losing_team   : chr  "Mexico" "Belgium" "Brazil" "Peru" ...
 $ date          : chr  "1930-07-13" "1930-07-13" "1930-07-14" "1930-07-14" ...
 $ month         : chr  "Jul" "Jul" "Jul" "Jul" ...
 $ dayofweek     : chr  "Sunday" "Sunday" "Monday" "Monday" ...
names(wcmatches)  #데이터 세트 변수(컬럼명) 보기
 [1] "year"           "country"        "city"           "stage"         
 [5] "home_team"      "away_team"      "home_score"     "away_score"    
 [9] "outcome"        "win_conditions" "winning_team"   "losing_team"   
[13] "date"           "month"          "dayofweek" 

연도별 개최 국가 

library(tidyverse)

no_of_host <- wcmatches %>%
  group_by(year, country) %>%   #연도와 개최국 기준으로Grouping 
  summarise(tmp_no = n())         # temp_no는 연도와 개최국 기준의 row 수로 의미는 없음


no_of_host <- no_of_host %>% 
  select(-tmp_no)                       #의미없는 temp_no 제외


no_of_host%>%
  head()  
# A tibble: 6 × 2
# Groups:   year [6]
   year country    
  <int> <chr>      
1  1930 Uruguay    
2  1934 Italy      
3  1938 France     
4  1950 Brazil     
5  1954 Switzerland
6  1958 Sweden 
no_of_host%>%
  tail()  
# A tibble: 6 × 2
# Groups:   year [5]
   year country     
  <int> <chr>       
1  2002 Japan       
2  2002 South Korea 
3  2006 Germany     
4  2010 South Africa
5  2014 Brazil      
6  2018 Russia  
  • 데이터 세트의 country는 경기가 개최된 국가 기준으로 2002 한일월드컵은 South Korean와 Japan이 모두 나옴 

 

상기 결과는 아래 파일로도 확인 가능

 

https://github.com/rfordatascience/tidytuesday/blob/master/data/2022/2022-11-29/worldcups.csv

worldcups <- readr::read_csv('https://raw.github.com/rfordatascience/tidytuesday/blob/master/data/2022/2022-11-29/worldcups.csv')
  • worldcups 데이터 세트에는 1930년 우루과이부터 2018년 러시아까지 월드컵 경기 요약 정보를 다룸
  • 데이터 세트에는 우승팀, 준우승팀, 3위팀, 4위팀 등의 정보를 제공
  • 공동 개최된 2002년 월드컵의 Host는 "Japan, South Korea"로 표시

worldcups.csv

variable class description
year double year  (월드컵 대회 년도)
host character host (개최 국가)
winner character winner (우승팀)
second character second (준우승팀)
third character third (3위팀)
fourth character fourth (4위팀)
goals_scored double goals_scored (전체 Goal 숭)
teams double teams (참가국 수)
games double games (경기 수)
attendance double attendance (관중 수)
year host winner second third fourth goals_
scored
teams games attendance
1930 Uruguay Uruguay Argentina USA Yugoslavia 70 13 18 434000
1934 Italy Italy Czechoslovakia Germany Austria 70 16 17 395000
1938 France Italy Hungary Brazil Sweden 84 15 18 483000
1950 Brazil Uruguay Brazil Sweden Spain 88 13 22 1337000
1954 Switzerland West Germany Hungary Austria Uruguay 140 16 26 943000
1958 Sweden Brazil Sweden France West Germany 126 16 35 868000
1962 Chile Brazil Czechoslovakia Chile Yugoslavia 89 16 32 776000
1966 England England West Germany Portugal Soviet Union 89 16 32 1614677
1970 Mexico Brazil Italy West Germany Uruguay 95 16 32 1673975
1974 Germany West Germany Netherlands Poland Brazil 97 16 38 1774022
1978 Argentina Argentina Netherlands Brazil Italy 102 16 38 1610215
1982 Spain Italy West Germany Poland France 146 24 52 1856277
1986 Mexico Argentina West Germany France Belgium 132 24 52 2407431
1990 Italy West Germany Argentina Italy England 115 24 52 2527348
1994 USA Brazil Italy Sweden Bulgaria 141 24 52 3568567
1998 France France Brazil Croatia Netherlands 171 32 64 2859234
2002 Japan, South Korea Brazil Germany Turkey South Korea 161 32 64 2724604
2006 Germany Italy France Germany Portugal 147 32 64 3367000
2010 South Africa Spain Netherlands Germany Uruguay 145 32 64 2167984
2014 Brazil Germany Argentina Netherlands Brazil 171 32 64 3441450
2018 Russia France Croatia Belgium England 169 32 64 3031768

국가별 개최횟수

wcmatches %>%
  group_by(year, country) %>%
  summarise(tmp_no = n()) %>%
  group_by(country) %>%
  summarise(host_no = n()) %>%
  arrange(-host_no)
# A tibble: 17 × 2
   country        host_no
   <chr>             <int>
 1 Brazil                  2
 2 France                2
 3 Germany            2
 4 Italy                    2
 5 Mexico               2
 6 Argentina           1
 7 Chile                  1
 8 England             1
 9 Japan                1
10 Russia              1
11 South Africa      1
12 South Korea     1
13 Spain                1
14 Sweden            1
15 Switzerland       1
16 United States    1
17 Uruguay            1
worldcups <- read.csv('worldcups.csv')

str(worldcups)
'data.frame': 21 obs. of  10 variables:
 $ year        : int  1930 1934 1938 1950 1954 1958 1962 1966 1970 1974 ...
 $ host        : chr  "Uruguay" "Italy" "France" "Brazil" ...
 $ winner      : chr  "Uruguay" "Italy" "Italy" "Uruguay" ...
 $ second      : chr  "Argentina" "Czechoslovakia" "Hungary" "Brazil" ...
 $ third       : chr  "USA" "Germany" "Brazil" "Sweden" ...
 $ fourth      : chr  "Yugoslavia" "Austria" "Sweden" "Spain" ...
 $ goals_scored: int  70 70 84 88 140 126 89 89 95 97 ...
 $ teams       : int  13 16 15 13 16 16 16 16 16 16 ...
 $ games       : int  18 17 18 22 26 35 32 32 32 38 ...
 $ attendance  : int  434000 395000 483000 1337000 943000 868000 776000 1614677 1673975 1774022 ...
worldcups %>%
  group_by(host) %>%
  summarise(
    host_no = n_distinct(year)
  ) %>%
  arrange (desc(host_no))
# A tibble: 16 × 2
   host                     host_no
   <chr>                      <int>
 1 Brazil                           2
 2 France                         2
 3 Germany                     2
 4 Italy                             2
 5 Mexico                        2
 6 Argentina                     1
 7 Chile                            1
 8 England                       1
 9 Japan, South Korea    1
10 Russia                        1
11 South Africa                1
12 Spain                          1
13 Sweden                      1
14 Switzerland                1
15 USA                           1
16 Uruguay                     1
  • wcmatches 데이터 세트는 country 기준으로 국가별 개최 횟수를 정리하니,
    한국과 일본이 구분되어 결과가 도출됨
    • 1930년 우르과이 대회(1회 )부터 2018년 러시아 대회(21회)의 총 개최국가는 17국
    • 2회 개최 국가 : 브라질, 프랑스, 독일, 이탈리아, 멕시코  (알파벳 순)
  • worldcups 데이터 세트는 host 기준으로 공동 개최한 2002년의 host가 Japan, South Korea 기준으로
    결과가 도출됨
    • 2002년 host가 Japan, South Korea로 16개 결과 도출
    • 2회 개최 국가 : 브라질, 프랑스, 독일, 이탈리아, 멕시코 (알파벳 순) (※ 상기 결과와 동일)

百聞不如一見 (백문불여일견) >>> 百聞不如一打(백문불여일타)

  • 프로그래밍 언어를 배우는 사람들은 자주 듣게 되는 말이다.
  • 관련 책과 유튜브 영상을 보는 것만으로는 배울 수 없다. 
  • 책과 유튜브 백번 보고 듣는 것보다 한번 쳐 보는 것이 더 빠르게 배울 수 있다.
  • 요즘에는 '백문이불여일타'라는 말을 골프도 쓴다고 하는데, 결국은 연습이 배우는 지름길이다.
  • 여러 번 하다보면, 점점 더 익숙해 진다.

 

728x90