我有一个物种数据集及其在100 x 200米范围内的粗略位置.数据框的位置部分不是我认为可用的格式.在这个100 x 200米的矩形中,有两百个10 x 10米的正方形,名为A到CV.在每个10×10平方内,有四个5×5米的正方形,分别命名为1,2,3和4(1位于2的南边,3的西边.4位于2的东边,3的北面).我想让R知道A是在(0,0),(10,0),(0,0)和(0,10)处有角的正方形,B就在A的北边并且有角( 0,10),(0,20),(10,10)和(10,20),K位于A的东边,在(10,0),(10,10),(20,对于所有10×10米的正方形,0)和(20,10)等等.另外,我想让R知道每个5 x 5米的正方形在100 x 200米的情节中的位置.
所以,我的数据框看起来像这样
10x10 5x5 Tree Diameter
A 1 tree1 4
B 1 tree2 4
C 4 tree3 6
D 3 tree4 2
E 3 tree5 3
F 2 tree6 7
G 1 tree7 12
H 2 tree8 1
I 2 tree9 2
J 3 tree10 8
K 4 tree11 3
L 1 tree12 7
M 2 tree13 5
Run Code Online (Sandbox Code Playgroud)
最终,我希望能够绘制100 x 200米的区域并且每个10 x 10米的方格显示树木的数量,或物种的数量,或总生物量.转换数据的最佳方法是什么?到R可用于绘图和可能分析的空间数据?
这是一个开始.
## set up a vector of all 10x10 position tags
tags10 <- c(LETTERS,
paste0("A",LETTERS),
paste0("B",LETTERS),
paste0("C",LETTERS[1:22]))
Run Code Online (Sandbox Code Playgroud)
将(例如)转换{"J",3}为相应子平方的中心的函数.
convpos <- function(pos10,pos5) {
## convert letters to major (x,y) positions
p1 <- as.numeric(factor(pos10,levels=tags10)) ## or use match()
p1.x <- ((p1-1) %% 10) *10+5 ## %% is modulo operator
p1.y <- ((p1-1) %/% 10)*10+5 ## %/% is integer division
## sort out sub-positions
p2.x <- ifelse(pos5 <=2,2.5,7.5) ## {1,2} vs {3,4} values
p2.y <- ifelse(pos5 %%2 ==1 ,2.5,7.5) ## odd {1,3} vs even {2,4} values
c(p1.x+p2.x,p1.y+p2.y)
}
Run Code Online (Sandbox Code Playgroud)
用法:
convpos("J",2)
convpos(mydata$tenbytenpos,mydata$fivebyfivepos)
Run Code Online (Sandbox Code Playgroud)
重要笔记:
switch到ifelse这个原因10x10)很可能会变成类似于将X10.10数据读入R中的内容:看到?data.frame和?check.names与@Ben Bolker所做的类似,这里是一个查找功能(尽管您可能需要转置一些东西以使标签与您描述的匹配).
tenbyten <- c(LETTERS[1:26],
paste0("A",LETTERS[1:26]),
paste0("B",LETTERS[1:26]),
paste0("C",LETTERS[1:22]))
tenbyten <- matrix(rep(tenbyten, each = 2), ncol = 10)
tenbyten <- t(apply(tenbyten, 1, function(x){rep(x, each = 2)}))
# the 1234 squares
squares <- matrix(c(rep(c(1,2),10),rep(c(4,3),10)), nrow = 20, ncol = 20)
# stick together into a reference grid
my.grid <- matrix(paste(tenbyten, squares, sep = "-"), nrow = 20, ncol = 20)
# a lookup function for the site grid
coordLookup <- function(tbt, fbf, .my.grid = my.grid){
x <- col(.my.grid) * 5 - 2.5
y <- row(.my.grid) * 5 - 2.5
marker <- .my.grid == paste(tbt, fbf, sep = "-")
list(x = x[marker], y = y[marker])
}
coordLookup("BB",2)
$x
[1] 52.5
$y
[1] 37.5
Run Code Online (Sandbox Code Playgroud)
如果这不是你想要的,那么也许你更喜欢一个SpatialPolygonsDataFrame,它有正确的多边形ID,你附加数据,等等.在这种情况下,谷歌周围的如何从头开始制作,并操纵在row()和col()功能,让您的多边形角,类似于在此查找功能,只返回给定的重心.
编辑:启动SPDF:
这是从功能示例中修改的,并且有望成为一个好的开始:
library(sp)
# really you have a 20x20 grid, counting the small ones.
# c(2.5,2.5) specifies the distance in any direction from the cell center
grd <- GridTopology(c(1,1), c(2.5,2.5), c(20,20)))
grd <- as.SpatialPolygons.GridTopology(grd)
# get centroids
coords <- coordinates(polys)
# make SPDF, with an extra column for your grid codes, taken from the above.
# you can add further columns to this data.frame(), using polys@data
polys <- SpatialPolygonsDataFrame(grd,
data=data.frame(x=coords[,1], y=coords[,2], my.ID = as.vector(my.grid),
row.names=getSpPPolygonsIDSlots(grd)))
Run Code Online (Sandbox Code Playgroud)