[英]Specifying a word followed by a specific word followed by max of 3 words in regex in R
[英]Extracting specific word followed by another in R
我有空缺職位的描述。 我想對他們進行評分,然后將其發布在相鄰的欄中。 可以通過提取文字說明中“成績:”旁邊的單詞來完成
模擬
structure(list(description = structure(2:1, .Label = c("Grade: L3 Position title bla bla bla",
"Head of xxxxxxxx Grade: L5 Last Date to Apply: 22nd July 2019"
), class = "factor"), division = structure(2:1, .Label = c("ABC",
"XYZ"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
要求的結果
Description Division Grade
sdsdsdsd XYZ L5
asdasdsadas ABC L3
我找到了這個解決方案,它可以解決這個問題,但不能放在專欄中。
您可以使用sub
並在"Grade"
之后的單詞前后提取0或多個空格的單詞:
sub(".*Grade\\s*:\\s*(\\w+).*", "\\1", df$description)
#[1] "L5" "L3"
您可以像這樣使用stringr包:
library(stringr)
df[,"Grade"] <- sub("Grade: ", "", str_extract(df$description, "Grade: [^ ]+"))
數據:
df <- structure(list(description = structure(2:1, .Label = c("Grade: L3 Position title bla bla bla",
"Head of xxxxxxxx Grade: L5 Last Date to Apply: 22nd July 2019"
), class = "factor"), division = structure(2:1, .Label = c("ABC",
"XYZ"), class = "factor")), class = "data.frame", row.names = c(NA,
-2L))
編輯:我剛剛看到評論中有更好的答案。 因此最好使用其中一個,因為它們不依賴額外的程序包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.