【问题标题】:R unnest_tokens elements from listR unnest_tokens 列表中的元素
【发布时间】:2020-04-12 11:50:47
【问题描述】:

我有这个:

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

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

【问题讨论】:

    标签: r token tidytext unnest


    【解决方案1】:

    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 
    

    【讨论】:

    • 感谢 H 1,它就像一个魅力,我在哪里可以学到这样的东西?我在哪里可以结束这个问题?
    • 谢谢......我没有看到关闭这篇文章的“勾号”......我只有三个方框,旁边是“活跃”、“最旧”、“投票” ",我点击了投票但什么也没有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2016-12-03
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多