2016-10-11 102 views
0

我试图避免运行〜250克拉克和埃文斯手动测试(clarkevans.test)。如何为clark和evans测试创建多个owin?

我有一个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)) 
+0

,如果你提供了到目前为止你已经尝试过什么,数据是如何组织的一些示例代码,这会更容易些。根据你所描述的内容,我会尽量给出一个答案。 –

+0

谢谢你回答我的问题。我添加了一些我的数据和代码。我没有尝试任何循环,因为我读过的大部分内容都避免使用它们。我确实尝试了你用“apply”给出的例子,但是我又有一个错误说,如果指定了xrange,yrange之一,那么两者都必须是。 – JZA

+0

我得到了应用方法的工作!非常感谢你的帮助,我很感激。 – JZA

回答

0

我会建议一个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 
0

原代码没有工作,因为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])