我有一个数据集,它有17854个不同级别的邮政编码分布在美国各地。在R中有什么函数或者我可以用什么方法来减少级别的数量吗?我需要把它减少到不到500个不同的水平。
谢谢你,米纳尔
发布于 2014-04-12 05:50:43
trunc( as.numeric( substr( zipvec, 1,3))/2 )发布于 2014-04-12 06:53:56
这些数据可能对你有用。它有人口、城市、县、长、州、类型等方面的数据。
> url <- "http://www.unitedstateszipcodes.org/zip_code_database.csv"
> tmp <- tempfile()
> download.file(url, tmp)
> zips <- read.csv("zip_code_database.csv")
> unlink(tmp)
> zip.sub <- zips[zips$type == "STANDARD",
c("zip", "state", "latitude", "longitude")]
> zip.sub[sample(1:nrow(zip.sub), 10, FALSE), ]
## zip state latitude longitude
## 12121 28649 NC 36.33 -81.23
## 4129 11359 NY 40.79 -73.77
## 22577 51026 IA 42.23 -96.09
## 40251 95480 CA 38.64 -123.37
## 23441 53804 WI 42.89 -90.92
## 40574 96022 CA 40.33 -122.45
## 13589 31548 GA 30.79 -81.66
## 40557 96001 CA 40.60 -122.46
## 40428 95757 CA 38.35 -121.42
## 4531 12205 NY 42.66 -73.79当我们应用@BondedDust提交的截断函数时,我们得到
> trn <- trunc(as.numeric(substr(zip.sub$zip, 1,3))/2)
> length(unique(trn))
## [1] 442https://stackoverflow.com/questions/23026610
复制相似问题