R에서 "-"로 결합 된 단어 만 식별하는 방법은 무엇입니까? [복제]

Dec 13 2020

R에 다음 텍스트가 있다고 가정 해 보겠습니다.

x = "The effects in the medium-term of an appreciation of the exchange rate are still to be carefully assessed in our projections. However, we can observe in the short-term that the our pogramme of purchasing asset-backed securities had a positive impact on overall economic activity"

다음 만 얻을 수있는 방법 :

# medium-term
# short-term
# asset-backed 

기본적으로 "-"로 연결된 단어 만 추출하면됩니다.

누구든지 나를 도울 수 있습니까?

감사!

답변

1 KarthikS Dec 13 2020 at 17:45

작동합니까 :

library(stringr)
str_extract_all(x, '\\b[a-z]+-[a-z]+\\b')[[1]]
[1] "medium-term"  "short-term"   "asset-backed"
1 RonakShah Dec 13 2020 at 17:47

기본 R에서는 다음을 사용할 수 있습니다.

regmatches(x, gregexpr('\\w+-\\w+', x))[[1]]
#[1] "medium-term"  "short-term"   "asset-backed"