【问题标题】:Unlist a list with more than one values in a row取消列出连续包含多个值的列表
【发布时间】:2020-06-15 21:11:53
【问题描述】:

我尝试从这些命令中创建一个取消列表:

library(quanteda)
library(tidyr)
df <- data.frame(id = c(1,2), text = c("I am loving it", "I am hating it but I go, and I teach"), stringsAsFactors = FALSE)

myDfm <- df$text %>%
      tokens(remove_punct = TRUE, remove_numbers = TRUE, remove_symbols = TRUE) %>%
      tokens_remove(pattern = c(stopwords(source = "smart")))

data.frame(id = c(1,2), text = c("loving", "hating teach")

这是预期输出的示例:

data.frame(id = 1:length(myDfm),text = unlist(myDfm))
  id         text
1  1       loving
2  2 hating teach

错误:

data.frame(id = 1:length(myDfm), text = unlist(myDfm)) 中的错误:
参数意味着不同的行数:2, 3

【问题讨论】:

  • data.frame(id = seq_along(myDfm), text = sapply(myDfm, paste, collapse = " "))

标签: r


【解决方案1】:

使用sapplypaste0

data.frame(id = 1:length(myDfm),text = sapply(myDfm,  paste0, collapse = " "))
      id         text
text1  1       loving
text2  2 hating teach

【讨论】:

    【解决方案2】:

    我们可以使用stack

    stack(lapply(myDfm, paste, collapse=" "))[2:1]
    #     ind       values
    #1 text1       loving
    #2 text2 hating teach
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 2018-10-07
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      相关资源
      最近更新 更多