0

I am trying to avoid running ~250 Clark and Evans tests (clarkevans.test) manually.

I have a table of xmin, xmax, ymin, ymax coordinates in an excel file where each row are the dimensions an operating window.

After reading the excel file (read.csv) into R I can't seem to get any form of "apply" and "owin" to work in conjunction to output an owin for each row. Eventually I'll need to create ppp and run clarkevans.test in a similar fashion, but right now I just need help on this first step.

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

I've tried several variations, but I can't get anything to work.

lapply(cdf, function(x) owin(xmin, xmax, ymin, ymax)) 
markalex
  • 8,623
  • 2
  • 7
  • 32
JZA
  • 1
  • 1
  • It would be easier if you provided some example code of what you have tried so far and how the data is organized. I will try to give an answer below based on what you have described. – Ege Rubak Oct 11 '16 at 07:50
  • Thank you for responding to my question. I added some of my data and code. I didn't try any for loops because most of the things I read said avoid using them. I did try the example you gave with "apply", but again I got an error saying, If one of xrange, yrange is specified then both must be. – JZA Oct 11 '16 at 19:50
  • I got the apply method to work! Thank you very much for your help, I appreciate it. – JZA Oct 11 '16 at 20:04
  • Great. Please consider accepting the answer so it doesn't show up as unanswered. Btw. if all you are doing is to read in 250 point pattens and calculating the Clark and Evans index I really doubt you will have any performance issues with using a for-loop (which may be easier to read and debug than a complicated apply approach). – Ege Rubak Oct 12 '16 at 09:21

2 Answers2

0

I would recommend a for-loop for this since you easily can add steps to generate ppp objects when you get that far:

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])
}

Then the list of owin objects contains exactly what you expect:

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

If you insist on using 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
Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
0

The original code didn't work because owin(xmin,xmax,ymin,ymax) is not valid syntax for calling owin.

One valid syntax is owin(c(xmin,xmax), c(ymin,ymax)).

The following would work on a data frame df whose columns are xmin,xmax,ymin,ymax:

apply(df, 1, function(z) owin(z[1:2], z[3:4])
Adrian Baddeley
  • 1,956
  • 1
  • 8
  • 7