Jak rozpoznać tylko słowa połączone znakiem „-” w R? [duplikować]
Dec 13 2020
Załóżmy, że mam następujący tekst w 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"
Jak mogę uzyskać tylko następujące informacje:
# medium-term
# short-term
# asset-backed
Zasadniczo potrzebowałbym wyodrębnić tylko te słowa, które są połączone znakiem „-”.
Czy ktoś może mi pomóc?
Dzięki!
Odpowiedzi
1 KarthikS Dec 13 2020 at 17:45
Czy to działa:
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
W podstawowym R możesz użyć:
regmatches(x, gregexpr('\\w+-\\w+', x))[[1]]
#[1] "medium-term" "short-term" "asset-backed"