从长格式到具有相同重复项的宽格式

From long to wide format with the same duplicates

正在尝试这个命令:

library("spacyr")
library("dplyr", warn.conflicts = FALSE)

mytext <- data.frame(text = c("test text", "section 2 sending"), 
                     id = c(32,41))
df2 <- tidyr::separate_rows(mytext, text)

df3 <- data.frame(text = df2$text, id = df2$id)

dflemma <- spacy_parse(structure(df3$text, names = df3$id),
                       lemma = TRUE, pos = FALSE)  %>%
    mutate(id = doc_id) %>%
    group_by(id) %>%
    summarize(body = paste(lemma, collapse = " "))

预期输出是从长到宽的格式,使用相同的 ID 并用 space 分隔合并文本。这里的预期输出

data.frame(text = c("test text", "section 2 send"), 
                     id = c(32,41)

但是命令提供了这个错误:

Error in process_document(x, multithread) : Docnames are duplicated.

在您的 df3 上尝试这个 base R 解决方案:

#Code
dflemma <- aggregate(text~id,df3,function(x) paste(x,collapse = ' '))

输出:

  id              text
1 32         test text
2 41 section 2 sending

您收到此错误消息是因为您将每个文本短语分隔为单词。你不应该那样做。考虑以下代码:

mytext <- data.frame(text = c("test text", "section 2 sending"), id = c(32,41))
dflemma <- 
  spacy_parse(structure(mytext$text, names = mytext$id), lemma = TRUE, pos = FALSE) %>% 
  group_by(id = doc_id) %>% 
  summarise(text = paste(lemma, collapse = " "))

输出

> dflemma
# A tibble: 2 x 2
  id    text          
  <chr> <chr>         
1 32    test text     
2 41    section 2 send

更新

如果必须进行分离,则需要进一步修改 id 列以确保其中的每个观察值都是唯一的。稍后您可以将那些 id 更改回 group_by 阶段。考虑以下代码。

mytext <- data.frame(text = c("test text", "section 2 sending"), id = c(32,41))
df2 <- tidyr::separate_rows(mytext, text) %>% group_by(id) %>% mutate(id = paste0(id, "-", seq_len(n())))
dflemma <- 
  spacy_parse(structure(df2$text, names = df2$id), lemma = TRUE, pos = FALSE) %>% 
  group_by(id = sub("(.+)-(.+)", "\1", doc_id)) %>% 
  summarise(text = paste(lemma, collapse = " "))