Вот еще одно решение в R.
#Packages needed
library(dplyr)
library(magrittr)
library(tidyr)
#Your data
home_team <- c("belgium", "brazil", "italy", "sweden",
"france", "brazil", "italy", "chile")
away_team <- c("france", "uruguay", "belgium", "mexico",
"chile", "england", "belgium", "switzerland")
home_score <- c(2,3,1,3,
3,2,1,2)
away_score <- c(2,1,2,1,
1,1,2,2)
year <- c(1990, 1990, 1990, 1990,
1991, 1991, 1991, 1991)
df <- data.frame(home_team, away_team, home_score, away_score, year, stringsAsFactors = FALSE)
df
# home_team away_team home_score away_score year
# 1 belgium france 2 2 1990
# 2 brazil uruguay 3 1 1990
# 3 italy belgium 1 2 1990
# 4 sweden mexico 3 1 1990
# 5 france chile 3 1 1991
# 6 brazil england 2 1 1991
# 7 italy belgium 1 2 1991
# 8 chile switzerland 2 2 1991
#Column names for the new data.frames
my_colnames <- c("team", "score", "year")
#Using select() to create separate home and away datasets
df_home <- df %>% select(matches("home|year")) %>% setNames(my_colnames) %>% mutate(game_where = "home")
df_away <- df %>% select(matches("away|year")) %>% setNames(my_colnames) %>% mutate(game_where = "away")
#rbind()'ing both data.frames
#Grouping the rows together first by the team and then by the year
#Summing up the scores for the aforementioned groupings
#Sorting the newly produced data.frame by year
df_1 <- rbind(df_home, df_away) %>% group_by(team, year) %>% tally(score) %>% arrange(year)
df_1
# team year n
# <chr> <dbl> <dbl>
# 1 belgium 1990 4
# 2 brazil 1990 3
# 3 france 1990 2
# 4 italy 1990 1
# 5 mexico 1990 1
# 6 sweden 1990 3
# 7 uruguay 1990 1
# 8 belgium 1991 2
# 9 brazil 1991 2
#10 chile 1991 3
#11 england 1991 1
#12 france 1991 3
#13 italy 1991 1
#14 switzerland 1991 2