Я полагаю, что функция to_categorical требует, чтобы входные данные были матрицей, что делает это работающим:
SentenceID = c(1,1,1,1,2,2,2,3,3,3,3,3,3,3,3)
Tokens = c("I","went","to","school","nobody","can","find","some","people","know","what","they","are","doing","now")
WordIndex = c(3,4,7,8,9,10,12,54,34,66,33,89,87,23,22)
TagIndex = c(1,3,2,4,1,3,4,1,2,4,3,4,2,3,4)
df = data.frame(SentenceID, Tokens, WordIndex, TagIndex)
lst <- split(df$WordIndex, f = df$SentenceID)
lstWord2 <- lapply(lst, function(x){
if (length(x) < 10){
x2 <- c(x, rep(0, 10 - length(x)))
}
return(x2)
})
lstTag <- split(df$TagIndex, f = df$SentenceID)
lstTag2 <- lapply(lstTag, function(x){
if (length(x) < 10){
x2 <- c(x, rep(0, 10 - length(x)))
}
return(x2)
})
y <- to_categorical(as.matrix(lstTag2), num_classes = NULL)
Я получил это:
> y
, , 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 1 1 1 1 1 1
[2,] 0 0 0 1 1 1 1 1 1 1
[3,] 0 0 0 0 0 0 0 0 1 1
, , 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 1 0 0 0 0 0 0 0 0 0
[3,] 1 0 0 0 0 0 0 0 0 0
, , 3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 1 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0 0
[3,] 0 1 0 0 0 1 0 0 0 0
, , 4
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 1 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0 0
[3,] 0 0 0 1 0 0 1 0 0 0
, , 5
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 1 0 0 0 0 0 0
[2,] 0 0 1 0 0 0 0 0 0 0
[3,] 0 0 1 0 1 0 0 1 0 0