File: aggregate.Rout.save

package info (click to toggle)
r-cran-sf 0.9-7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,796 kB
  • sloc: cpp: 5,333; sh: 18; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 4,820 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.2, PROJ 6.2.1
> # aggregate
> pl1 = st_polygon(list(rbind(c(0,0),c(1,0),c(1,1),c(0,0))))
> pl2 = st_polygon(list(rbind(c(0,0),c(1,1),c(0,1),c(0,0))))
> s = st_sf(a = 1:2, geom = st_sfc(pl1, pl2))
> (a = aggregate(s, list(c(1,1)), mean, do_union = FALSE))
Simple feature collection with 1 feature and 2 fields
Attribute-geometry relationship: 0 constant, 1 aggregate, 1 identity
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS:            NA
  Group.1   a                       geometry
1       1 1.5 MULTIPOLYGON (((0 0, 1 0, 1...
> (a = aggregate(s, list(c(1,1)), mean, do_union = TRUE))
Simple feature collection with 1 feature and 2 fields
Attribute-geometry relationship: 0 constant, 1 aggregate, 1 identity
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS:            NA
  Group.1   a                       geometry
1       1 1.5 POLYGON ((1 1, 1 0, 0 0, 0 ...
> # expect_warning(st_cast(a, "POINT"))
> demo(meuse_sf, echo = FALSE, ask = FALSE)
> a = aggregate(meuse_sf, list(meuse_sf$soil), mean)
There were 12 warnings (use warnings() to see them)
> attributes(a)$agr
  Group.1   cadmium    copper      lead      zinc      elev      dist        om 
 identity aggregate aggregate aggregate aggregate aggregate aggregate aggregate 
    ffreq      soil      lime   landuse    dist.m 
aggregate aggregate aggregate aggregate aggregate 
Levels: constant aggregate identity
> a = aggregate(meuse_sf, list(soil = meuse_sf$soil), mean)
There were 12 warnings (use warnings() to see them)
> attributes(a)$agr
     soil   cadmium    copper      lead      zinc      elev      dist        om 
 identity aggregate aggregate aggregate aggregate aggregate aggregate aggregate 
    ffreq    soil.1      lime   landuse    dist.m 
aggregate aggregate aggregate aggregate aggregate 
Levels: constant aggregate identity
> a = aggregate(meuse_sf, list(meuse_sf$soil, meuse_sf$ffreq), mean)
There were 32 warnings (use warnings() to see them)
> attributes(a)$agr
  Group.1   Group.2   cadmium    copper      lead      zinc      elev      dist 
 identity  identity aggregate aggregate aggregate aggregate aggregate aggregate 
       om     ffreq      soil      lime   landuse    dist.m 
aggregate aggregate aggregate aggregate aggregate aggregate 
Levels: constant aggregate identity
> a = aggregate(meuse_sf, list(soil = meuse_sf$soil, ff = meuse_sf$ffreq), mean)
There were 32 warnings (use warnings() to see them)
> attributes(a)$agr
     soil        ff   cadmium    copper      lead      zinc      elev      dist 
 identity  identity aggregate aggregate aggregate aggregate aggregate aggregate 
       om     ffreq    soil.1      lime   landuse    dist.m 
aggregate aggregate aggregate aggregate aggregate aggregate 
Levels: constant aggregate identity
> 
> # aggregate by sf/sfc
> a = st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)))) * 2
> b = a + 1
> p = st_sfc(st_point(c(0.1,0.1)), st_point(c(1.5,1.5)), st_point(c(2.9,2.9)))
> x = st_sf(count = 1:3, geom = p)
> aggregate(x, st_sfc(a,b), mean)
Simple feature collection with 2 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 3 ymax: 3
CRS:            NA
  count                       geometry
1   1.5 POLYGON ((0 0, 2 0, 2 2, 0 ...
2   2.5 POLYGON ((1 1, 3 1, 3 3, 1 ...
> aggregate(x, st_sf(st_sfc(a,b)), mean)
Simple feature collection with 2 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 3 ymax: 3
CRS:            NA
  count                       geometry
1   1.5 POLYGON ((0 0, 2 0, 2 2, 0 ...
2   2.5 POLYGON ((1 1, 3 1, 3 3, 1 ...
> aggregate(x, st_sf(st_sfc(a,b,b+10)), mean)
Simple feature collection with 3 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 0 ymin: 0 xmax: 13 ymax: 13
CRS:            NA
  count                       geometry
1   1.5 POLYGON ((0 0, 2 0, 2 2, 0 ...
2   2.5 POLYGON ((1 1, 3 1, 3 3, 1 ...
3    NA POLYGON ((11 11, 13 11, 13 ...
> 
> proc.time()
   user  system elapsed 
  0.403   0.036   0.429