R语言 重新格式化街道地址

iqxoj9l9  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(188)

我有一个房产的地址,它被分成了许多字段。对于公寓(公寓)的房产,街道名称和编号的格式不理想,无法与其他信息匹配:

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))

我想做的是将属性“编号”从街道名称的末尾移到开头。
我觉得规则是这样的:

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.
euoag5mw

euoag5mw1#

下面是一个例子:

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"

相关问题