Examples for 'sf::tidyverse'


Tidyverse methods for sf objects

Aliases: tidyverse filter.sf arrange.sf group_by.sf ungroup.sf rowwise.sf mutate.sf transmute.sf select.sf rename.sf rename_with.sf slice.sf summarise.sf summarise distinct.sf gather.sf pivot_longer.sf pivot_wider.sf spread.sf sample_n.sf sample_frac.sf group_split.sf nest.sf separate.sf separate_rows.sf unite.sf unnest.sf drop_na.sf inner_join.sf left_join.sf right_join.sf full_join.sf semi_join.sf anti_join.sf

Keywords:

### ** Examples

if (require(dplyr, quietly = TRUE)) {
 nc = read_sf(system.file("shape/nc.shp", package="sf"))
 nc %>% filter(AREA > .1) %>% plot()
 # plot 10 smallest counties in grey:
 st_geometry(nc) %>% plot()
 nc %>% select(AREA) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey')
 title("the ten counties with smallest area")
 nc2 <- nc %>% mutate(area10 = AREA/10)
 nc %>% slice(1:2)
}
Attaching package: 'dplyr'
The following object is masked from 'package:rcloud.support':

    select
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
Warning: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot
all
plot of chunk example-sf-tidyverse-1
plot of chunk example-sf-tidyverse-1
Simple feature collection with 2 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -80.90344 ymax: 36.58965
Geodetic CRS:  NAD27
# A tibble: 2 × 15
   AREA PERIMETER CNTY_ CNTY_ID NAME   FIPS  FIPSNO CRESS_ID BIR74 SID74 NWBIR74
  <dbl>     <dbl> <dbl>   <dbl> <chr>  <chr>  <dbl>    <int> <dbl> <dbl>   <dbl>
1 0.114      1.44  1825    1825 Ashe   37009  37009        5  1091     1      10
2 0.061      1.23  1827    1827 Alleg… 37005  37005        3   487     0      10
# … with 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,
#   geometry <MULTIPOLYGON [°]>
# plot 10 smallest counties in grey:
if (require(dplyr, quietly = TRUE)) {
 st_geometry(nc) %>% plot()
 nc %>% select(AREA) %>% arrange(AREA) %>% slice(1:10) %>% plot(add = TRUE, col = 'grey')
 title("the ten counties with smallest area")
}
if (require(dplyr, quietly = TRUE)) {
 nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
 nc %>% group_by(area_cl) %>% class()
}
[1] "sf"         "grouped_df" "tbl_df"     "tbl"        "data.frame"
if (require(dplyr, quietly = TRUE)) {
 nc2 <- nc %>% mutate(area10 = AREA/10)
}
if (require(dplyr, quietly = TRUE)) {
 nc %>% transmute(AREA = AREA/10) %>% class()
}
[1] "sf"         "tbl_df"     "tbl"        "data.frame"
if (require(dplyr, quietly = TRUE)) {
 nc %>% select(SID74, SID79) %>% names()
 nc %>% select(SID74, SID79) %>% class()
}
[1] "sf"         "tbl_df"     "tbl"        "data.frame"
if (require(dplyr, quietly = TRUE)) {
 nc2 <- nc %>% rename(area = AREA)
}
if (require(dplyr, quietly = TRUE)) {
 nc %>% slice(1:2)
}
Simple feature collection with 2 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -80.90344 ymax: 36.58965
Geodetic CRS:  NAD27
# A tibble: 2 × 16
   AREA PERIMETER CNTY_ CNTY_ID NAME   FIPS  FIPSNO CRESS_ID BIR74 SID74 NWBIR74
  <dbl>     <dbl> <dbl>   <dbl> <chr>  <chr>  <dbl>    <int> <dbl> <dbl>   <dbl>
1 0.114      1.44  1825    1825 Ashe   37009  37009        5  1091     1      10
2 0.061      1.23  1827    1827 Alleg… 37005  37005        3   487     0      10
# … with 5 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,
#   geometry <MULTIPOLYGON [°]>, area_cl <fct>
if (require(dplyr, quietly = TRUE)) {
 nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
 nc.g <- nc %>% group_by(area_cl)
 nc.g %>% summarise(mean(AREA))
 nc.g %>% summarise(mean(AREA)) %>% plot(col = grey(3:6 / 7))
 nc %>% as.data.frame %>% summarise(mean(AREA))
}
plot of chunk example-sf-tidyverse-1
  mean(AREA)
1    0.12626
if (require(dplyr, quietly = TRUE)) {
 nc[c(1:100, 1:10), ] %>% distinct() %>% nrow()
}
[1] 100
if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE) && "geometry" %in% names(nc)) {
 nc %>% select(SID74, SID79) %>% gather("VAR", "SID", -geometry) %>% summary()
}
Attaching package: 'tidyr'
The following object is masked from 'package:RCurl':

    complete
          geometry       VAR                 SID        
 MULTIPOLYGON :200   Length:200         Min.   : 0.000  
 epsg:4267    :  0   Class :character   1st Qu.: 2.000  
 +proj=long...:  0   Mode  :character   Median : 5.000  
                                        Mean   : 7.515  
                                        3rd Qu.: 9.000  
                                        Max.   :57.000  
if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE) && "geometry" %in% names(nc)) {
 nc$row = 1:100 # needed for spread to work
 nc %>% select(SID74, SID79, geometry, row) %>%
        gather("VAR", "SID", -geometry, -row) %>%
        spread(VAR, SID) %>% head()
}
Simple feature collection with 6 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
Geodetic CRS:  NAD27
# A tibble: 6 × 4
                                                      geometry   row SID74 SID79
                                            <MULTIPOLYGON [°]> <int> <dbl> <dbl>
1 (((-81.47276 36.23436, -81.54084 36.27251, -81.56198 36.273…     1     1     0
2 (((-81.23989 36.36536, -81.24069 36.37942, -81.26284 36.405…     2     0     3
3 (((-80.45634 36.24256, -80.47639 36.25473, -80.53688 36.256…     3     5     6
4 (((-76.00897 36.3196, -76.01735 36.33773, -76.03288 36.3359…     4     1     2
5 (((-77.21767 36.24098, -77.23461 36.2146, -77.29861 36.2115…     5     9     3
6 (((-76.74506 36.23392, -76.98069 36.23024, -76.99475 36.235…     6     7     5
if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) {
 storms.sf = st_as_sf(storms, coords = c("long", "lat"), crs = 4326)
 x <- storms.sf %>% group_by(name, year) %>% nest
 trs = lapply(x$data, function(tr) st_cast(st_combine(tr), "LINESTRING")[[1]]) %>%
    st_sfc(crs = 4326)
 trs.sf = st_sf(x[,1:2], trs)
 plot(trs.sf["year"], axes = TRUE)
}
plot of chunk example-sf-tidyverse-1

[Package sf version 1.0-19 Index]