【问题标题】:Extract numbers after a pattern in vector of characters在字符向量中的模式之后提取数字
【发布时间】:2020-01-14 21:18:52
【问题描述】:

我正在尝试从字符串向量中提取值。向量中的每个字符串,(向量中大约有 2300 个)遵循以下示例的模式:

"733|Overall (-2 to 2): _________2________________|How controversial is each sentence (1-5)?|Sent. 1 (ANALYSIS BY...): ________1__________|Sent. 2 (Bail is...): ____3______________|Sent. 3 (2) A...): _______1___________|Sent. 4 (3) A...): _______1___________|Sent. 5 (Proposition 100...): _______5___________|Sent. 6 (In 2006,...): _______3___________|Sent. 7 (That legislation...): ________1__________|Types of bias (check all that apply):|Pro   Anti|X      O   Word use (bold, add alternate)|X      O   Examples (italicize)|O      O   Extra information (underline)|X      O   Any other bias (explain below)|Last sentence makes it sound like an urgent matter.|____________________________________________|NA|undocumented, without a visa|NA|NA|NA|NA|NA|NA|NA|NA|"  

我想要的是提取模式“Sent.”之后的数字并将它们放入一个单独的向量中。例如,我想提取“1311531”。

我无法使用 gsub 来完成此操作。

【问题讨论】:

  • 所有数字 总是 0 到 9 之间的整数吗?
  • @OrlandoSabogal:是的。

标签: r regex gsub


【解决方案1】:

图书馆(tidyverse)

Data <- c("PASTE YOUR WHOLE STRING")

str_locate(Data, "Sent. ")
Reference <- str_locate_all(Data, "Sent. ") %>% as.data.frame()
Reference %>% names() #Returns [1] "start" "end"  
Reference <- Reference %>% mutate(end = end +1)

YourNumbers <- substr(Data,start = Reference$end[1], stop = Reference$end[1])

for (i in 2:dim(Reference)[1]){
  Temp <- substr(Data,start = Reference$end[i], stop = Reference$end[i])
  YourNumbers <- paste(YourNumbers, Temp, sep = "")
}

YourNumbers #Returns "1234567"

【讨论】:

    【解决方案2】:

    我们可以使用stringr 中的str_match_all 来获取"Sent" 后跟的所有数字。

    str_match_all(text, "Sent.*?_+(\\d+)")[[1]][, 2]
    #[1] "1" "3" "1" "1" "5" "3" "1"
    

    【讨论】:

    • 这运行得非常好而且非常快。除了 for 循环之外,您对如何使其在字符串向量上运行有什么建议吗?
    • @StanO str_match_all 已经矢量化。因此,如果您在 text 中有字符串向量,str_match_all(text, "Sent.*?_+(\\d+)") 将返回一个矩阵列表。您需要提取每个矩阵的第二列。所以使用sapply/lapply 就像sapply(str_match_all(text, "Sent.*?_+(\\d+)"), function(x) x[, 2])
    • 一个快速的额外问题,如果我想修改您的代码以捕获没有下划线的数字,我该怎么做?例如。如果在发送之后。 1 它只是“1”而不是“________1__________”。
    • 在这种情况下,提取数字的规则是什么?如果我们在"Sent number" 之后选择下一个数字,我们可以执行str_match_all(text, "Sent\\.\\s+\\d+.*?(\\d+).*?\\|")[[1]][, 2],但它会错误地捕获一些数字,因为还有其他数字。
    【解决方案3】:

    使用strsplitsub 的基本R 选项

    lapply(strsplit(ss, "\\|"), function(x)
        sub("Sent.+: _+(\\d+)_+", "\\1", x[grepl("^Sent", x)]))
    #[[1]]
    #[1] "1" "3" "1" "1" "5" "3" "1"
    

    样本数据

    ss <- "733|Overall (-2 to 2): _________2________________|How controversial is each sentence (1-5)?|Sent. 1 (ANALYSIS BY...): ________1__________|Sent. 2 (Bail is...): ____3______________|Sent. 3 (2) A...): _______1___________|Sent. 4 (3) A...): _______1___________|Sent. 5 (Proposition 100...): _______5___________|Sent. 6 (In 2006,...): _______3___________|Sent. 7 (That legislation...): ________1__________|Types of bias (check all that apply):|Pro   Anti|X      O   Word use (bold, add alternate)|X      O   Examples (italicize)|O      O   Extra information (underline)|X      O   Any other bias (explain below)|Last sentence makes it sound like an urgent matter.|____________________________________________|NA|undocumented, without a visa|NA|NA|NA|NA|NA|NA|NA|NA|"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多