gpt4 book ai didi

r - 在 R 中将地址字符串拆分为城市、州和地址

转载 作者:行者123 更新时间:2023-12-04 12:36:52 27 4
gpt4 key购买 nike

我有一个下面给出的字符串形式的地址:

dat = data.frame(Addresses = c("1626 Aviation Way, Albuquerque, NM 30906, USA", 
"1626 Aviation Way, Augusta, GA 30906, USA",
"325 Main St, Stratford, CT 06615, USA",
"4205 Bessie Coleman Blvd, Tampa, FL 33607, USA"), stringsAsFactors = FALSE)

我想把它分成 5 列,如街道、城市、州、邮政编码、邮政。
我怎样才能在 R 中做到这一点。

最佳答案

我用一行代码解决了它。对于正则表达式专家来说可能看起来有点幼稚,但对于它的示例数据来说却是有效的。

library(stringr)

dat = data.frame(Addresses = c("1626 Aviation Way, Albuquerque, NM 30906, USA",
"1626 Aviation Way, Augusta, GA 30906, USA",
"325 Main St, Stratford, CT 06615, USA",
"4205 Bessie Coleman Blvd, Tampa, FL 33607, USA"), stringsAsFactors = FALSE)

str_match(dat$Addresses,"(.+), (.+), (.+) (.+), (.+)")[ ,-1]
[,1] [,2] [,3] [,4] [,5]
[1,] "1626 Aviation Way" "Albuquerque" "NM" "30906" "USA"
[2,] "1626 Aviation Way" "Augusta" "GA" "30906" "USA"
[3,] "325 Main St" "Stratford" "CT" "06615" "USA"
[4,] "4205 Bessie Coleman Blvd" "Tampa" "FL" "33607" "USA"

关于r - 在 R 中将地址字符串拆分为城市、州和地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com