【问题标题】:How to extract the text from certain places in the file?如何从文件中的某些位置提取文本?
【发布时间】:2021-09-18 20:14:31
【问题描述】:

这是我的数据:

文本

我怎样才能得到如下数据框:

9 ABCD 先生。内容1 9 DEFG 夫人。内容2 8 DBC 先生。内容3

3 行,4 个变量(编号、先生/夫人、姓名、内容)

我的数据中的名字总是在 Mr. 或 Mrs. 之后,并且总是大写。在我想要的内容之前总是有一段时间。

一般来说我想知道谁说了什么(带有数字标签)

谢谢!

【问题讨论】:

    标签: r text


    【解决方案1】:

    我们可以做

    library(stringr)
    library(tidyr)
    library(dplyr)
    tibble(col1 = text) %>% 
       separate_rows(col1, sep = "(?<=Content\\d\\.)\\s+") %>% 
       mutate(grp = readr::parse_number(col1)) %>%
       fill(grp) %>% 
       mutate(col1 = str_c(grp, str_remove(col1, "^[*]+\\d+\\s*"),
                sep=" "), grp = NULL) %>%
        pull(col1)
    

    -输出

    [1] "9 Mr.ABCD. Content1."               "9 Mrs. DEFG.Content2."              "8 Mr.DBC something else. Content3."
    

    数据

    text <- "**9 Mr.ABCD. Content1. Mrs. DEFG.Content2. **8 Mr.DBC something else. Content3."
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多