我试图避免手动运行约 250 个 Clark 和 Evans 测试 (clarkevans.test)。
我在 Excel 文件中有一个 xmin、xmax、ymin、ymax 坐标表,其中每一行都是操作窗口的尺寸。
将 Excel 文件(read.csv)读入 R 后,我似乎无法获得任何形式的“apply”和“owin”结合使用来为每行输出 owin。最终我需要创建 ppp 并以类似的方式运行 clarkevans.test,但现在我只需要第一步的帮助。
coordin<-read.csv("Coordin.csv")
cdf<-data.frame(coordin)
> cdf
xmin xmax ymin ymax
1 456741 456841 3913505 3913605
2 453341 453441 3915805 3915905
3 453441 453541 3915805 3915905
4 452441 452541 3915705 3915805
5 453741 453841 3915705 3915805
我尝试了几种变体,但没有任何效果。
lapply(cdf, function(x) owin(xmin, xmax, ymin, ymax))
我建议使用 for 循环,因为您可以轻松添加步骤 当你到达那么远时生成
ppp
对象:
library(spatstat)
# Test data:
dat <- data.frame(xmin = 1:3, xmax = 2:4, ymin = 1:3, ymax = 2:4)
# List of owin initialised as unit squares:
win_list <- replicate(nrow(dat), owin(), simplify = FALSE)
# For loop to make each owin:
for(i in seq_len(nrow(dat))){
# Vector of owin values:
v <- as.numeric(dat[i, ])
# Finally create the owin object
win_list[[i]] <- owin( v[1:2], v[3:4])
}
然后
owin
对象列表恰好包含您所期望的内容:
win_list
#> [[1]]
#> window: rectangle = [1, 2] x [1, 2] units
#>
#> [[2]]
#> window: rectangle = [2, 3] x [2, 3] units
#>
#> [[3]]
#> window: rectangle = [3, 4] x [3, 4] units
如果您坚持使用apply:
apply(dat, 1, function(x) owin(c(x[1], x[2]), c(x[3], x[4])))
#> [[1]]
#> window: rectangle = [1, 2] x [1, 2] units
#>
#> [[2]]
#> window: rectangle = [2, 3] x [2, 3] units
#>
#> [[3]]
#> window: rectangle = [3, 4] x [3, 4] units
原始代码不起作用,因为
owin(xmin,xmax,ymin,ymax)
对于调用owin
来说是无效语法。
一种有效的语法是
owin(c(xmin,xmax), c(ymin,ymax))
。
以下内容适用于数据框
df
,其列为 xmin,xmax,ymin,ymax
:
apply(df, 1, function(z) owin(z[1:2], z[3:4])