Ed_Gravy
Ed_Gravy

Reputation: 2033

Convert sf to marked ppp

I successfully converted an sf object to ppp using the code below:

sf_owin = maptools::as.ppp.SpatialPointsDataFrame(as_Spatial(sf_points__flat))

sf_points__flat looks like this:

Simple feature collection with 131 features and 3 fields
geometry type:  MULTIPOINT
dimension:      XY
bbox:           xmin: -1932934 ymin: 4958872 xmax: -1439558 ymax: 5861173
projected CRS:  NAD83(2011) / UTM zone 16N
# A tibble: 131 x 4
# Groups:   COOPID [131]
   COOPID STATION_NA                         geometry Annual_Precipitation
 *  <dbl> <chr>                      <MULTIPOINT [m]>                <dbl>
 1      0 Ontario                ((-1899685 5335073))                 9.24
 2 100010 ABERDEEN EXPERIMNT STN ((-1610453 5091311))                12.4 
 3 100227 AMERICAN FALLS 3 NW    ((-1623401 5075011))                20.4 
 4 100282 ANDERSON DAM           ((-1807106 5212322))                16.3 
 5 100347 ARBON 2 NW             ((-1606302 5034484))                10.2 
 6 100375 ARCO                   ((-1622855 5179969))                19.5 
 7 100448 ARROWROCK DAM          ((-1834338 5254236))                20.1 
 8 100470 ASHTON                 ((-1458491 5179214))                37.5 
 9 100528 AVERY RS #2            ((-1678382 5654084))                25.3 
10 100667 BAYVIEW MODEL BASIN    ((-1691954 5753129))                 9.69
# ... with 121 more rows

sf_owin looks like this: enter image description here

Marked planar point pattern: 131 points
Mark variables: COOPID, STATION_NA, Annual_Precipitation 
window: rectangle = [-1932934.3, -1439558.2] x [4958872, 5861173] units

Now when I try to create a ppp mark type I get an error

Stations_ppp_types = split(sf__owin, which.marks = "Type")
Error in split.ppp(sf__owin, which.marks = "Type") : 
  Data frame of marks contains no factors

How to split a ppp by marks(STATION_NA) by converting an sf object to ppp

Upvotes: 1

Views: 587

Answers (1)

Adrian Baddeley
Adrian Baddeley

Reputation: 2963

This question is about the spatstat package.

The error message indicates that the split command is dispatched to split.ppp, so you could look up the help for split.ppp to figure out the problem.

The help file would tell you that the argument which.marks is ignored by split.ppp. So you can remove that argument.

To split the point pattern, you need a factor (a vector of categorical values which specify the grouping). The marks in the point pattern are not factors. (This is what the error message is saying.) According to your printed output, the column STATION_NA is character valued. If you want to split the pattern according to these values, you need to convert them to factor values. For example

f <- marks(sf_owin)$"STATION_NA"
f <- factor(f)
Y <- split(sf_owin, f)

or you could repair the marks in the point pattern first, so that split.ppp would work automatically:

marks(sf_owin)$"STATION_NA" <- factor(marks(sf_owin)$"STATION_NA")
Y <- split(sf_owin)

Upvotes: 3

Related Questions