我是 R 新手,过去 2 个月一直在这个网站上尝试了解更多信息。我想从具有特定关键字的数据集中提取信息,然后从具有该关键字的数据集中提取信息,我想提取该关键字之前和之后的 5 个单词。然后我想知道在同一句话中他们附近有哪些数字。
为了解释“为什么”,我有一个门票列表,我想提取门票的所有标题。然后我想从请求额外存储的票证列表中了解这些票证。如果是,我想知道他们要求多少存储空间,然后我将根据他们要求的存储空间创建操作(但那是稍后的事)。
到目前为止我已完成的代码示例(有点困惑,我仍在研究更好/更干净的方法,我对 R 还很陌生)。
我正在搜索的关键字:存储
数据框引用为: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])
最佳答案
以下方法将提取特定关键字之前(本例中我使用 AND)或关键字之后的所有数字。您可以更改正则表达式模式中的关键字。
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
关于r - 如何提取特定关键字前后的所有数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63253346/