Как удалить один и тот же кусок текста из разных текстов в символьном векторе? - PullRequest
1 голос
/ 15 октября 2019

У меня есть символьный вектор, содержащий 231 документ (231 строка на один столбец). В начале каждого документа есть фрагмент текста, который я хотел бы удалить из каждого из 231 документа.

Давайте рассмотрим пример, где каждый текст имеет следующее начало: Текст, который я хочу удалить .:

Я пробовал следующие варианты без результатов:

x <- c("Text that I wish to remove. I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out.", "Text that I wish to remove. I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out.", "Text that I wish to remove. I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out.", "Text that I wish to remove. I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out.")
strings <- substr(x, 1, 26)

remove_1 <- x %>% str_replace_all(strings)

remove_2 <- gsub(strings, "", x)
ecb_remove <- str_remove_all(ecb_ready, ecb_strings)

В идеале хотелось бы получить:

[1] "I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out."
[2] "I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out."
[3] "I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out."
[4] "I really want to remove the text but I cannot do it. I hope that stackoverflow will sort it out."

Может ли кто-нибудь мне помочь?

Большое спасибо!

1 Ответ

1 голос
/ 22 октября 2019

Если строка, которую вы хотите удалить, имеет фиксированную длину, вы можете использовать:

substring(x, 29)

или, если она имеет фиксированный шаблон, вы можете использовать:

sub("^Text that I wish to remove. ", "", x)

В случаеВы хотите удалить все до первого ., который вы можете использовать:

sub("^.*?\\. ", "", x)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...