r语言 - 重新格式化街道地址



我有一个属性的地址,它被分割成许多字段。对于公寓(公寓)的属性,街道名称和编号的理想形式不是为了与其他信息匹配:

Address1 <- c("76", "Flat 2", "Flat 4")
Address2 <- c("Acacia Avenue", "Hightown, 23", "London Road, 34A")
# Not ideal for flats
print(paste(Address1, Address2))
Address3 < -c("Acacia Avenue", "23, Hightown", "34A, London Road")
# Better for flats
print(paste(Address1, Address3))

我想移动属性"number"从街道名称的末尾到开始

我觉得规则是这样的:

looking at the end of the string:
is there a ", " followed by a number or a number plus a single character?
if so then move the contents after the "," to the start of the string.

下面是一个例子:

Address2 <- c("Acacia Avenue", "Hightown, 23", "London Road, 34A", "Flat 2")
gsub("(.*), (\d+[A-Za-z]?$)" ,"\2 \1", Address2)
#> [1] "Acacia Avenue"   "23 Hightown"     "34A London Road" "Flat 2"

最新更新