1suppressPackageStartupMessages(library(sf))
2
3library(dplyr)
4options(dplyr.summarise.inform=FALSE)
5read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>%
6	st_transform(3857) -> nc
7nc %>% filter(AREA > .1) %>% plot()
8
9# plot 10 smallest counties in grey:
10nc %>%
11  select(BIR74, geometry) %>%
12  plot()
13
14nc %>%
15  select(AREA, geometry) %>%
16  arrange(AREA) %>%
17  slice(1:10) %>%
18  plot(add = TRUE, col = 'grey', main ="")
19
20# select: check both when geometry is part of the selection, and when not:
21nc %>% select(SID74, SID79) %>% names()
22nc %>% select(SID74, SID79, geometry) %>% names()
23nc %>% select(SID74, SID79) %>% class()
24nc %>% select(SID74, SID79, geometry) %>% class()
25
26# group_by:
27nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
28nc %>% group_by(area_cl) %>% class()
29
30# mutate:
31nc2 <- nc %>% mutate(area10 = AREA/10)
32
33# transmute:
34nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class()
35nc %>% transmute(AREA = AREA/10) %>% class()
36
37# rename:
38nc2 <- nc %>% rename(area = AREA)
39
40# distinct:
41nc[c(1:100,1:10),] %>% distinct() %>% nrow()
42
43# summarize:
44nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
45nc.g <- nc %>% group_by(area_cl)
46nc.g %>% summarise(mean(AREA))
47nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7)
48
49library(tidyr)
50
51# time-wide to long table, using tidyr::gather
52# stack the two SID columns for the July 1, 1974 - June 30, 1978 and July 1, 1979 - June 30, 1984 periods
53# (see https://cran.r-project.org/web/packages/spdep/vignettes/sids.pdf)
54nc %>% select(SID74, SID79, geometry) %>% gather("VAR", "SID", -geometry) %>% summary()
55
56# spread:
57nc$row = 1:100
58nc.g <- nc %>% select(SID74, SID79, row) %>% gather("VAR", "SID", -row, -geometry)
59nc.g %>% tail()
60nc.g %>% spread(VAR, SID) %>% head()
61nc %>% select(SID74, SID79, geometry, row) %>% gather("VAR", "SID", -geometry, -row) %>% spread(VAR, SID) %>% head()
62
63# test st_set_crs in pipe:
64sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1)))
65x <- sfc %>% st_set_crs(4326) %>% st_transform(3857)
66x
67
68read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>%
69	st_transform(3857) -> nc
70nc.merc <- st_transform(nc, 32119) # NC State Plane
71suppressPackageStartupMessages(library(units))
72install_unit("person")
73person = as_units("person")
74nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person / area)
75
76# summary(nc.merc$dens) # requires units 0.4-2
77nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25))
78nc.grp <- nc.merc %>% group_by(area_cl)
79
80out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area),
81	new_dens = sum(dens * area)/sum(area))
82
83# mean densities depend on grouping:
84nc.merc %>% summarize(mean(dens))
85out %>% summarise(mean(new_dens))
86
87# total densities don't:
88nc.merc %>% summarise(sum(area * dens))
89out %>% summarise(sum(A * new_dens))
90
91conn = system.file("gpkg/nc.gpkg", package = "sf")
92
93library(DBI)
94library(RSQLite)
95con = dbConnect(SQLite(), dbname = system.file("gpkg/nc.gpkg", package = "sf"))
96dbReadTable(con, "nc.gpkg") %>% filter(AREA > 0.2) %>% collect %>% st_sf
97
98# nest:
99storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326)
100x <- storms.sf %>% group_by(name, year) %>% nest
101
102nrow(distinct(nc[c(1,1,1,2,2,3:100),]))
103
104# set.seed(1331)
105nc$gp <- sample(10, 100, replace=TRUE)
106# Get centroid of each group of polygons; https://github.com/r-spatial/sf/issues/969
107nc_gp_cent <- nc %>%
108                group_by(gp) %>%
109                group_map(st_area)
110
111nc %>% st_filter(nc[1,]) %>% nrow
112