R 中的 CountVectorizer 无法将所有单词放入包中

CountVectorizer in R not fitting all the words into the bag

我有这样一个数据框:

    id                 words
 1:  1          capuccin,mok
 2:  2 bimboll,ext,sajonjoli
 3:  3          burrit,sincr
 4:  4  div,tir,mini,doradit
 5:  5   pan,multigran,linaz
 6:  6         tost,integral
 7:  7             pan,blanc
 8:  8  sup,pan,bco,ajonjoli
 9:  9                  wond
10: 10                  wond

我正在使用以下代码:

bag_of_words <- CountVectorizer$new()
result_df <- cbind(df$id, bag_of_words$fit_transform(df$words))

我想要这样的东西:

   tab_1$id capuccin mok bimboll ext sajonjoli...
1         1        1   1       0   0         0...
2         2        0   0       1   1         1...
3         3        0   0       0   0         0...
4       ...      ... ...     ... ...       ...

但是,它 returns 是一个包含每个单词出现次数的矩阵,它只是返回单词 wond:

   df$id wond
1         1    0
2         2    0
3         3    0
4         4    0
5         5    0
6         6    0
7         7    0
8         8    0
9         9    1
10       10    1

我的代码有什么问题?

我是通过使用比评论中 tmfmnk 建议的相似方法得到的。

tab_1 <- tab_1 %>%
  unnest(words) %>%
  mutate(words = strsplit(words, ','), occ = 1) %>%
  dcast(id ~ unlist(words), fill = 0)

现在一切正常。

id ajonjoli bco bimboll ...
1         0   0       0 ...
2         0   0       1 ...
3         0   0       0 ...
...     ... ...     ...