R unnest_tokens 列表中的元素

R unnest_tokens elements from list

我有这个:

library(tidytext)
list_chars <- list("you and I", "he or she", "we and they")
list_chars_as_tibble <- lapply(list_chars, tibble)
list_chars_by_word <- lapply(list_chars_as_tibble, unnest_tokens)

知道了:

Error in check_input(x) : 
  Input must be a character vector of any length or a list of character
  vectors, each of which has a length of 1.

想要得到这个:

[[1]]
1 you
2 and
3 I

[[2]]
1 he
2 or
3 she

[[3]]
1 we
2 and
3 they

请帮忙,我相信我已经尝试了所有方法,但显然不行,谢谢

unnest_tokens() 需要被告知解析哪一列,所以你需要在你的小标题中命名字符列:

library(tidytext)
library(tibble)

list_chars_as_tibble <- lapply(list_chars, function(x) tibble(txt = x))
lapply(list_chars_as_tibble, unnest_tokens, word, txt)

[[1]]
# A tibble: 3 x 1
  word 
  <chr>
1 you  
2 and  
3 i    

[[2]]
# A tibble: 3 x 1
  word 
  <chr>
1 he   
2 or   
3 she  

[[3]]
# A tibble: 3 x 1
  word 
  <chr>
1 we   
2 and  
3 they