我正在try 使用新的separate_wider_regex()
函数来分隔字符串.
在第一个例子(moldavia_1
)中,我们有一个模式.因此,获取所有列很简单:
moldavia_1 <-
tibble(adresa = c("1;MD-3101,Balti str-la Botu Pavel 3",
"3;MD-3102,Balti str-la Muresanu A. 11",
"17;MD-3102,Balti str-la Sorocii 46",
"398;MD-3111,Balti str-la Stefan cel Mare 20",
"1130;MD-3128,Balti str-la Lvovului 2",
"1252;MD-3128,Balti str-la Lvovului 1",
"2814;MD-3102,Balti str-la Cahulului 44"))
使用separate_wider_regex()
:
moldavia_1 %>%
separate_wider_regex(cols = adresa,
patterns = c(ids = "^\\d+",
";",
cod_post = ".*",
",",
cod_4 = "\\w+",
"\\s",
str = "str-la",
den_str = "\\s[A-Z][a-z]+.*(?<=[a-z]|[A-Z][:punct:])\\s(?=[0-9])",
nr = "\\d+$"))
# A tibble: 7 × 6
ids cod_post cod_4 str den_str nr
<chr> <chr> <chr> <chr> <chr> <chr>
1 1 MD-3101 Balti str-la " Botu Pavel " 3
2 3 MD-3102 Balti str-la " Muresanu A. " 11
3 17 MD-3102 Balti str-la " Sorocii " 46
4 398 MD-3111 Balti str-la " Stefan cel Mare " 20
5 1130 MD-3128 Balti str-la " Lvovului " 2
6 1252 MD-3128 Balti str-la " Lvovului " 1
7 2814 MD-3102 Balti str-la " Cahulului " 44
在第二个示例(moldavia_2
)中,如果模式失败(在本例中是列"str"),则子序列列也会失败.
moldavia_2 <-
tibble(adresa = c("1;MD-3101,Balti Botu Pavel 3",
"3;MD-3102,Balti str-la Muresanu A. 11",
"17;MD-3102,Balti Sorocii 46",
"398;MD-3111,Balti Stefan cel Mare 20",
"1130;MD-3128,Balti str-la Lvovului 2",
"1252;MD-3128,Balti str-la Lvovului 1",
"2814;MD-3102,Balti Cahulului 44"))
使用separate_wider_regex()
:
moldavia_2 %>%
separate_wider_regex(cols = adresa,
patterns = c(ids = "^\\d+",
";",
cod_post = ".*",
",",
cod_4 = "\\w+",
"\\s",
str = "str-la",
den_str = "\\s[A-Z][a-z]+.*(?<=[a-z]|[A-Z][:punct:])\\s(?=[0-9])",
nr = "\\d+$"),
too_few = "align_start")
# A tibble: 7 × 6
ids cod_post cod_4 str den_str nr
<chr> <chr> <chr> <chr> <chr> <chr>
1 1 MD-3101 Balti NA NA NA
2 3 MD-3102 Balti str-la " Muresanu A. " 11
3 17 MD-3102 Balti NA NA NA
4 398 MD-3111 Balti NA NA NA
5 1130 MD-3128 Balti str-la " Lvovului " 2
6 1252 MD-3128 Balti str-la " Lvovului " 1
7 2814 MD-3102 Balti NA NA NA
我期待着:
# A tibble: 7 × 6
ids cod_post cod_4 tip_str den_str nr
<chr> <chr> <chr> <chr> <chr> <chr>
1 1 MD-3101 Balti NA Botu Pavel 3
2 3 MD-3102 Balti str-la Muresanu A. 11
3 17 MD-3102 Balti NA Sorocii 46
4 398 MD-3111 Balti NA Stefan cel Mare 20
5 1130 MD-3128 Balti str-la Lvovului 2
6 1252 MD-3128 Balti str-la Lvovului 1
7 2814 MD-3102 Balti NA Cahulului 44