Reputation: 95
Here is the code for a matrix that I am trying to construct:
Data3 = read.csv(filename, stringsAsFactors = FALSE)
Data3
v <- LETTERS[1:10]
A <- matrix(0, nrow = nrow(Data3),ncol = 5)
A
A[cbind(1:nrow(Data3),match(Data3$AwayTeam,v))] <- 1
A[cbind(1:nrow(Data3),match(Data3$HomeTeam,v))] <- -1
A
Essentially, it will be a 20x10 matrix, where every "away team" will be denoted by a "1" and every home team will be a "-1."
This bit of code has worked fine every other time I have used it, but for some reason now I am met with the "subscript out of bounds" error. Any ideas on how to fix this?
Upvotes: 0
Views: 228
Reputation: 2414
v has length 10, so the match(data$AwayTeam, v)
can give an index >5 if the away team is >E, but the matrix A only has 5 columns so attempting to set those values falls outside the size of the array. I'd guess either previous csv's only had teams A..E or ncol=5 has changed from a previous ncol=10.
Upvotes: 1