我想根据两列纬度和经度在 R 中创建一个唯一 ID,以便重复的位置具有相同的集群 ID。
例如:
LAT LONG Cluster_ID
13.5330 -15.4180 1
13.5330 -15.4180 1
13.5330 -15.4180 1
13.5330 -15.4180 1
13.5330 -15.4170 2
13.5330 -15.4170 2
13.5330 -15.4170 2
13.5340 -14.9350 3
13.5340 -14.9350 3
13.5340 -15.9170 4
13.3670 -14.6190 5
这是一种使用方法interaction
.
d <- read.table(text='LAT LONG
13.5330 -15.4180
13.5330 -15.4180
13.5330 -15.4180
13.5330 -15.4180
13.5330 -15.4170
13.5330 -15.4170
13.5330 -15.4170
13.5340 -14.9350
13.5340 -14.9350
13.5340 -15.9170
13.3670 -14.6190', header=TRUE)
d <- transform(d, Cluster_ID = as.numeric(interaction(LAT, LONG, drop=TRUE)))
# LAT LONG Cluster_ID
# 1 13.533 -15.418 2
# 2 13.533 -15.418 2
# 3 13.533 -15.418 2
# 4 13.533 -15.418 2
# 5 13.533 -15.417 3
# 6 13.533 -15.417 3
# 7 13.533 -15.417 3
# 8 13.534 -14.935 4
# 9 13.534 -14.935 4
# 10 13.534 -15.917 1
# 11 13.367 -14.619 5
编辑:纳入@Spacedman的建议以提供drop=TRUE
to interaction
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)