【问题标题】:How to extract all numbers before and after a specific keyword?如何提取特定关键字前后的所有数字?
【发布时间】:2020-08-04 18:51:04
【问题描述】:

我是 R 新手,过去 2 个月一直在这个网站上尝试了解更多信息。我想从具有特定关键字的数据集中提取信息,然后从具有该关键字的数据集中提取信息,我想提取该关键字前后的 5 个单词。然后我想知道在同一句话中他们附近有哪些数字。

为了解释“为什么”,我有一个门票列表,我想提取门票的所有标题。然后我想从那些请求额外存储的票的列表中知道。如果是,我想知道他们需要多少存储空间,然后我会根据他们需要多少存储空间来创建操作(但那是以后的事了)。

到目前为止我已经完成的代码示例(有点混乱,我仍在研究一种更好/更清洁的方式,我对 R 很陌生)。

我要搜索的关键字:存储

Dataframe 引用为:DF、DF2、DF3 等

来自 DF 的列:标题

#Check for keyword#
grep("storage", DF$Title, ignore.case=true)

#Pull words before and after keywords, this is case sensitive for some reason so I have to do it twice and merge the data frames, it also creates a list instead of a data frame so I have to change that into a data frame...Messy I know#
DF2 <- stringr::str_extract_all(DF$Title, "([^\\s]+\\s){0,5}Storage(\\s[^\\s]+){0,5}")

#Turn list into dataframe#
DF3 <- do.call(rbind.data.frame, DF2)

#Pull words before and after but in lower case, same as step two#
DF4 <- stringr::str_extract_all(DF$Title, "([^\\s]+\\s){0,5}storage(\\s[^\\s]+){0,5}")

#Turn list into dataframe#
DF5 <- do.call(rbind.data.frame, DF4)

#Change column names ( I have to do this to merge them via rbind)
DF6 <- setnames(DF3, c("Keyword")
DF7 <- setnames(DF5, c("Keyword")

#Merge both data frames together#
DF6 <- rbind(DF6,Df7)

我想检查请求的存储量,因此我正在尝试查找引用 GB 或 TB 等的数字。我尝试了很多代码,但很多代码只是在关键字后面提取数字或数字,而不是句子中的所有数字。

我尝试过的不工作示例

DFTest <- as.integer(str_match(DF6, "(?i\\bGB:?\\s*(\\d+")[,2])

【问题讨论】:

  • 如果不知道DF 的样子,我们就无法测试任何东西。请提供来自dput(head(DF)) 的输出,确保有一些匹配和一些不匹配的行。

标签: r dataframe numbers extract stringr


【解决方案1】:

以下方法将提取特定关键字之前或关键字之后的所有数字(本例中我使用了 AND)。您可以在 regex 模式中更改关键字。

library(tidyverse)

df <- data.frame(obs = 1:5, COL_D = c("2019AND", "AND1999", "101AND", "AND12", "20AND1999999"))

df2 <- df %>% 
  mutate(Extracted_Num = str_extract_all(COL_D, regex("\\d+(?=AND)|(?<=AND)\\d+")))

# obs        COL_D Extracted_Num
# 1   1      2019AND          2019
# 2   2      AND1999          1999
# 3   3       101AND           101
# 4   4        AND12            12
# 5   5 20AND1999999   20, 1999999

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多