I am trying to create a list of landscape objects, which have been grouped according to a variable called sim. Ideally I want a list with landscape objects, that can be called i.e.
[[1]]
Planar point pattern: 100 points
window: rectangle = [0, 1000] x [0, 1000] units
[[2]]
Planar point pattern: 100 points
window: rectangle = [0, 1000] x [0, 1000] units
The code I've tried so far is this:
library(tidyr)
library(spatstat)
set.seed(23)
x<-runif(1000)*1000
y<-runif(1000)*1000
sim<-rep(1:10,100)
coordinates<-data.frame(x,y,sim)
coordinates<-coordinates[order(sim),]
emptylist<-vector(mode="list",length=10)
metriccalculation<-function(x,y){
emptylist<-ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))
}
coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y))
This returns the error:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'coordinates' of mode 'function' was not found
EDIT 1: I tried adding emptylist as a variable in the metriccalculation function, returning the same error.
metriccalculation<-function(x,y,r){
r<-ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))
}
coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y,emptylist))
EDIT 2: I tried adding an append to the function so that the list is filled. This also returned the same error.
emptylist<-vector(mode="list",length=10)
metriccalculation<-function(x,y,r){
r<-append(r,(ppp(x,y,owin(xrange=c(0,1000),yrange=c(0,1000)))))
}
coordinates%>%group_by(sim)%>%lapply(coordinates,function(x)metriccalculation(coordinates$x,coordinates$y,emptylist))
EDIT 3:
Okay, I've checked online and there is a possible way of passing multiple variables through an apply function. This leads to:
coordinates%>%group_by(sim)%>%apply(coordinates,metriccalculation,x=coordinates$x,y=coordinates$y,r=emptylist)
And a new error,
Error in d[-MARGIN] : invalid subscript type 'list'