Вопрос
После идентификации элементов в символьном векторе, который удовлетворяет нецифровому шаблону, как я могу вставить те элементы, которые последовательно удовлетворяют шаблону regex ?
Обзор
sample_text
в основном содержит два шаблона:
- нечетные элементы: содержат 6 цифр, за которыми следует текст;
- четные элементы: содержат 4 цифры, за которыми следует текст.
Однако, есть несколько случаев нецифрового текста, которые принадлежат нечетному элементу, который предшествует ему.
Предыдущее решение
Раньше я не знал о последовательных нецифровых элементах в векторе символов. Это позволило мне вручную вставить каждый нецифровый элемент в элемент, который находился непосредственно на два элемента за нецифровым элементом.
# load necessary package
library(tidyverse) --------
# load necessary data ------
sample_text <-
c("811411 Home and Garden Equipment Repair and"
, "7699 Repair Services, Nec"
, "Maintenance" # non-digit pattern
, "811412 Appliance Repair and Maintenance"
, "7623 Refrigeration Service and Repair"
, "811412 Appliance Repair and Maintenance"
, "7629 Electrical Repair Shops")
# previous solution -------
sample_text %>%
# for those elements which satisfy the non-digit pattern
# identify the index of those elements which are exactly two behind
replace(list = str_detect(., "^\\D*$") %>% which() - 2
# of those elements which are exactly two behind the non-digit pattern
# paste the non-digit pattern to the end of them.
, values = paste(.[str_detect(., "^\\D*$") %>% which() - 2]
, str_subset(., "^\\D*$"))) %>%
# only keep elements with digits
str_subset("\\d")
# [1] "811411 Home and Garden Equipment Repair and Maintenance" # successfully copied the non-digit element and pasted it two elements behind
# [2] "7699 Repair Services, Nec"
# [3] "811412 Appliance Repair and Maintenance"
# [4] "7623 Refrigeration Service and Repair"
# [5] "811412 Appliance Repair and Maintenance"
# [6] "7629 Electrical Repair Shops"
# end of script #
Текущая проблема
Теперь, когда я понимаю, что sample_text
содержит последовательные нецифровые элементы, я не уверен, как обновить свое предыдущее решение. Любая помощь будет высоко ценится!
# sample data -----
sample_text <-
c("811310 Commercial and Industrial Machinery and"
, "7692 Welding Repair"
, "Equipment (except Automotive and" # non-digit pattern (1/2)
, "Electronic) Repair and Maintenance" # non-digit pattern (2/2)
, "811310 Commercial and Industrial Machinery and"
, "7694 Armature Rewinding Shops"
, "Equipment (except Automotive and" # non-digit pattern (1/2)
, "Electronic) Repair and Maintenance" # non-digit pattern (2/2)
, "811310 Commercial and Industrial Machinery and"
, "7699 Repair Services, Nec"
, "Equipment (except Automotive and" # non-digit pattern (1/2)
, "Electronic) Repair and Maintenance" # non-digit pattern (2/2)
, "811411 Home and Garden Equipment Repair and"
, "7699 Repair Services, Nec"
, "Maintenance" # non-digit pattern (1/1)
, "811412 Appliance Repair and Maintenance"
, "7623 Refrigeration Service and Repair"
, "811412 Appliance Repair and Maintenance"
, "7629 Electrical Repair Shops"
, "811412 Appliance Repair and Maintenance"
, "7699 Repair Services, Nec")
# desired output ------
[1] "811310 Commercial and Industrial Machinery and Equipment (except Automotive and Electronic) Repair and Maintenance"
[2] "7692 Welding Repair"
[3] "811310 Commercial and Industrial Machinery and Equipment (except Automotive and Electronic) Repair and Maintenance"
[4] "7694 Armature Rewinding Shops"
[5] "811310 Commercial and Industrial Machinery and Equipment (except Automotive and Electronic) Repair and Maintenance"
[6] "7699 Repair Services, Nec"
[7] "811411 Home and Garden Equipment Repair and Maintenance"
[8] "7699 Repair Services, Nec"
[9] "811412 Appliance Repair and Maintenance"
[10] "7623 Refrigeration Service and Repair"
[11] "811412 Appliance Repair and Maintenance"
[12] "7629 Electrical Repair Shops"
[13] "811412 Appliance Repair and Maintenance"
[14] "7699 Repair Services, Nec"