R:将纬度和经度划分为网格部分

时间:2017-04-19 00:41:45

标签: r

我有纬度和经度点:

> d1 <- data.frame(lat, lon)
> head(d1)
       lat       lon
1 43.25724 -96.01955
2 43.25724 -95.98172
3 43.25724 -95.92336
4 43.25616 -96.40973
5 43.25616 -96.25733
6 43.25616 -96.17735

其中有413个。我想(两种说法相同的方式):

  • 根据纬度和经度将它们分为9组(排列成网格)。

  • 在lat与lon的情节上绘制像tic-tac-toe board一样的网格线,并根据它们落入的网格单元将这些点划分为bin(stratify)。

如果我想将纬度划分为9组,我可以使用cut函数like this,但我实际上是在寻找cut的二维版本。

编辑:

使用建议:我该如何绘制这个?

paste(cut(lat, 3, labels=FALSE), cut(lon, 3, labels=FALSE))
  [1] "3 3" "3 3" "3 3" "3 1" "3 2" "3 2" "3 3" "3 3" "3 2" "3 1" "3 2" "3 1" "3 2"
 [14] "3 3" "3 3" "3 1" "3 3" "3 2" "3 2" "3 2" "3 1" "3 3" "3 1" "3 1" "3 3" "3 2"
 [27] "3 2" "3 2" "3 1" "3 2" "3 1" "3 3" "3 1" "3 3" "3 1" "3 2" "3 3" "3 2" "3 2"
 [40] "3 3" "3 3" "3 2" "3 2" "3 2" "3 3" "3 1" "3 3" "3 3" "3 3" "3 2" "3 3" "3 3"
 [53] "3 2" "3 2" "3 3" "3 3" "3 1" "3 2" "3 1" "3 2" "3 2" "3 2" "3 3" "3 2" "3 3"
 [66] "3 3" "3 3" "3 3" "3 3" "3 3" "3 3" "3 1" "3 2" "3 3" "3 1" "3 1" "3 1" "3 1"
 [79] "3 2" "3 2" "3 2" "3 1" "3 3" "3 2" "3 2" "3 2" "3 3" "3 3" "3 1" "3 3" "3 1"
 [92] "3 3" "3 3" "3 1" "3 3" "3 1" "3 3" "3 1" "3 2" "3 3" "3 3" "3 2" "3 2" "3 1"
[105] "3 1" "3 3" "3 2" "3 2" "3 3" "3 3" "3 3" "3 2" "3 1" "3 1" "3 2" "3 2" "3 2"
[118] "3 1" "3 1" "3 2" "3 3" "3 2" "3 2" "3 3" "3 2" "3 1" "3 3" "3 3" "3 1" "3 3"
[131] "3 1" "3 1" "3 3" "2 2" "2 2" "2 1" "2 1" "2 2" "2 3" "2 1" "2 2" "2 2" "2 3"
[144] "2 1" "2 2" "2 3" "2 3" "2 2" "2 3" "2 3" "2 2" "2 2" "2 3" "2 2" "2 1" "2 2"
[157] "2 2" "2 3" "2 3" "2 1" "2 1" "2 2" "2 1" "2 1" "2 1" "2 3" "2 2" "2 3" "2 3"
[170] "2 3" "2 2" "2 3" "2 3" "2 2" "2 1" "2 1" "2 1" "2 2" "2 2" "2 2" "2 2" "2 2"
[183] "2 3" "2 1" "2 2" "2 2" "2 3" "2 3" "2 2" "2 2" "2 3" "2 2" "2 2" "2 2" "2 1"
[196] "2 3" "2 1" "2 2" "2 3" "2 3" "2 1" "2 3" "2 3" "2 1" "2 2" "2 1" "2 2" "2 3"
[209] "2 1" "2 3" "2 2" "2 2" "2 2" "2 3" "2 2" "2 1" "2 2" "2 2" "2 3" "2 3" "2 3"
[222] "2 2" "2 3" "2 2" "2 1" "2 1" "2 2" "2 2" "2 3" "2 2" "2 3" "2 2" "2 2" "2 1"
[235] "2 2" "2 2" "2 3" "2 2" "2 3" "2 3" "2 3" "2 3" "2 1" "2 1" "2 2" "2 2" "2 3"
[248] "2 1" "2 2" "2 3" "2 2" "2 3" "2 3" "2 1" "2 1" "2 3" "2 3" "2 1" "2 3" "2 1"
[261] "2 1" "2 1" "2 3" "2 1" "2 2" "2 2" "2 2" "2 3" "2 3" "2 1" "2 1" "2 2" "2 3"
[274] "2 3" "2 2" "2 2" "2 1" "1 2" "1 2" "1 3" "1 3" "1 1" "1 1" "1 2" "1 2" "1 2"
[287] "1 2" "1 1" "1 3" "1 3" "1 2" "1 1" "1 1" "1 1" "1 2" "1 1" "1 1" "1 3" "1 2"
[300] "1 2" "1 2" "1 3" "1 1" "1 3" "1 1" "1 3" "1 2" "1 1" "1 2" "1 2" "1 2" "1 1"
[313] "1 3" "1 1" "1 1" "1 2" "1 3" "1 1" "1 2" "1 1" "1 2" "1 1" "1 3" "1 2" "1 2"
[326] "1 1" "1 2" "1 3" "1 3" "1 1" "1 2" "1 3" "1 3" "1 1" "1 3" "1 3" "1 1" "1 2"
[339] "1 2" "1 2" "1 3" "1 1" "1 2" "1 3" "1 2" "1 3" "1 3" "1 1" "1 2" "1 2" "1 1"
[352] "1 1" "1 2" "1 2" "1 3" "1 3" "1 1" "1 2" "1 2" "1 3" "1 1" "1 2" "1 2" "1 3"
[365] "1 1" "1 2" "1 1" "1 3" "1 3" "1 1" "1 1" "1 2" "1 2" "1 3" "1 1" "1 3" "1 1"
[378] "1 3" "1 3" "1 1" "1 1" "1 2" "1 3" "1 2" "1 1" "1 2" "1 3" "1 3" "1 2" "1 2"
[391] "1 3" "1 1" "1 2" "1 2" "1 3" "1 2" "1 2" "1 3" "1 1" "1 3" "1 1" "1 2" "1 2"
[404] "1 2" "1 1" "1 3" "1 1" "1 2" "1 1" "1 1" "1 1" "1 3" "1 1"
> 

问题是纬度和经度必须位于同一网格部分。我可能错了,但看起来并不像这里发生的那样。

编辑2:出现问题......获得NAs。

> df2 <- data.frame(lat, lon)
> df2 <- within(df2, {
+   grp.lat = cut(lat, (0:3)/3, labels = FALSE)
+   grp.lon = cut(lon, (0:3)/3, labels = FALSE)
+ })
> head(df2)
       lat       lon grp.lon grp.lat
1 43.25724 -96.01955      NA      NA
2 43.25724 -95.98172      NA      NA
3 43.25724 -95.92336      NA      NA
4 43.25616 -96.40973      NA      NA
5 43.25616 -96.25733      NA      NA
6 43.25616 -96.17735      NA      NA

最终解决方案:

#Divide the dataset into equally-sized chunks, as evenly as possible, for 9 chunks (coarse)

df2 <- data.frame(lat, lon)
df2 <- within(df2, {
  grp.lat = cut(lat, 3, labels = FALSE)
  grp.lon = cut(lon, 3, labels = FALSE)
})
head(df2)

#Want the minimum lon value for which grp.lon = 1 and the maximum lon value for which grp.lon=1

start_grp1_lon <- min(df2$lon[df2$grp.lon==1])
start_grp2_lon <- min(df2$lon[df2$grp.lon==2])
start_grp3_lon <- min(df2$lon[df2$grp.lon==3])

start_grp1_lat <- min(df2$lat[df2$grp.lat==1])
start_grp2_lat <- min(df2$lat[df2$grp.lat==2])
start_grp3_lat <- min(df2$lat[df2$grp.lat==3])

plot(lat ~ lon, data = df2, pch = (15:23)[grp.lon*grp.lat], col=[grp.lon]) #pch = (15:17)[grp.lon], col = grp.lat)
abline(v = c(start_grp1_lon, start_grp2_lon, start_grp3_lon))
abline(h = c(start_grp1_lat, start_grp2_lat, start_grp3_lat))

1 个答案:

答案 0 :(得分:2)

一种方法是在每个轴上使用cut

set.seed(2)
n <- 50
df <- data.frame(x = runif(n), y = runif(n))
head(df)
#           x           y
# 1 0.1848823 0.007109038
# 2 0.7023740 0.014693911
# 3 0.5733263 0.683403423
# 4 0.1680519 0.929720222
# 5 0.9438393 0.275401199
# 6 0.9434750 0.811859695

现在分配垃圾箱,任意生成一个3x3网格:

df <- within(df, {
  grp.x = cut(x, (0:3)/3, labels = FALSE)
  grp.y = cut(y, (0:3)/3, labels = FALSE)
})
head(df)
#           x           y grp.y grp.x
# 1 0.1848823 0.007109038     1     1
# 2 0.7023740 0.014693911     1     3
# 3 0.5733263 0.683403423     3     2
# 4 0.1680519 0.929720222     3     1
# 5 0.9438393 0.275401199     1     3
# 6 0.9434750 0.811859695     3     3

现在这些可以用于分组,着色等。这里的图表仅用于演示,但表示通过颜色(Y轴)和形状(X轴),这些点可以作为一个处理基。

enter image description here

plot(y ~ x, data = df, pch = (15:17)[grp.x], col = grp.y)
abline(v = (1:2)/3)
abline(h = (1:2)/3)