Comment identifier uniquement les mots unis par «-» dans R? [dupliquer]

Dec 13 2020

Supposons que j'ai le texte suivant dans 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"

Comment puis-je obtenir uniquement les éléments suivants:

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

Fondamentalement, je n'aurais besoin d'extraire que les mots liés par un "-".

Quelqu'un peut-il m'aider?

Merci!

Réponses

1 KarthikS Dec 13 2020 at 17:45

Est-ce que ça marche:

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

Dans la base R, vous pouvez utiliser:

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