Вы можете попробовать код ниже
v <- LETTERS[1:4]
A <- matrix(0,nrow = nrow(df),ncol = 4)
A[cbind(1:nrow(df),match(df$AwayTeam,v))] <- 1
A[cbind(1:nrow(df),match(df$HomeTeam,v))] <- -1
, такой что
> A
[,1] [,2] [,3] [,4]
[1,] 1 -1 0 0
[2,] 0 0 1 -1
[3,] 1 0 0 -1
[4,] 0 1 -1 0
[5,] -1 0 1 0
[6,] 0 -1 0 1
ДАННЫЕ
df <- structure(list(week = c(1L, 1L, 2L, 2L, 3L, 3L), AwayTeam = c("A",
"C", "A", "B", "C", "D"), AwayScore = c(1L, 0L, 1L, 1L, 4L, 2L
), HomeTeam = c("B", "D", "D", "C", "A", "B"), HomeScore = c(1L,
1L, 0L, 0L, 0L, 2L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))