R Programming Language 📍

R is a programming language and free software environment for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. It is widely used among statisticians and data miners for developing statistical software and data analysis.

📽️ Videos

You’ll like arcpullr

If you ever need to do GIS work with R using an remote ArcGIS server … you’ll like arcpullr

With arcpullr you can for example get a shapefile of Schenectady EDs with all of four lines of code, only one if you don’t count loading the libraries. It also can do seamless spatial queries using get_layer_by_polygon(), so you can pull only part of layer your interested in, which is good for large layers like tax maps.

library(arcpullr)
library(tidyverse)
library(sf)

get_spatial_layer('https://spatialags.vhb.com/arcgis/rest/services/29816_SIMS/SIMS/MapServer/54') %>% write_sf('/tmp/schenectady_eds.shp')

MapColoring

MapColoring

The MapColoring package provides functions for assigning colors to polygon maps such that no two adjacent features have the same color, while using a minimal number of colors. If provided more than the minimal required number of colors for a given map, it selects colors in a manner that maximizes color contrasts between adjacent polygons. The package also implements the DSATUR graph coloring algorithm in R.

Spend a good portion of yesterday exploring code for finding peaks in a digital elevation model

Spend a good portion of yesterday exploring code for finding peaks in a digital elevation model. I tried a few different methods, one searching for highest points within the x and y axis, looking for inclines and declines, and picking the points in the middle, but that found significantly more points then desired as it seems there is a lot of small up and downs in the elevation, even if they aren’t the actual summit.

What I ended up settling on was this code, that first finds the highest point in the DEM, then deletes all points within 100 meters in all directions from a copy of the DEM then recurvisely calls the function. This works fairly well, although it is memory intensive after about 10 calls, as you are making a lot of copies of DEM in memory. But it seems to work well, assuming you set a reasonable size for the buffer based on the size of the mountain.

library(tidyverse)
library(terra)
library(units)
library(sf)
rm(list=ls())

dem <- rast('/tmp/merge_5925072950_2_meter.tif')
demdf <- as.data.frame(dem, xy=T) %>%
mutate(Layer_1 = Layer_1 %>% set_units('m') %>% set_units('ft')) %>% drop_units()

dems <- aggregate(dem, fact=4)
demsdf <- as.data.frame(dems, xy=T) %>%
mutate(Layer_1 = Layer_1 %>% set_units('m') %>% set_units('ft')) %>% drop_units()


findPeaks <- function(df, size=100, n=5, peakdf=NA) {
dfp <- df
newPeak <- dfp %>% slice_max(Layer_1)

ifelse(is.na(peakdf), peakdf <- newPeak, peakdf <- rbind(peakdf, newPeak))

dfp <-
dfp %>%
filter( !(between(x, newPeak[1,1]-size, newPeak[1,1]+size) &
between(y, newPeak[1,2]-size, newPeak[1,2]+size)))

if (nrow(peakdf) >= n) {
return(peakdf)
}
else {
findPeaks(dfp, size, n, peakdf)
}
}


findPeaks(demdf, 300, 10) %>%
st_as_sf(coords=c('x', 'y'), crs=26918, agr = "constant") %>%
ggplot() +
geom_raster(data=demsdf, aes(x=x, y=y, fill=Layer_1)) +
geom_sf(color='white') +
ggsflabel::geom_sf_label_repel(aes(label=floor(Layer_1)))

 

Big Book of R

Big Book of R

Thanks for stopping by. If you’re like me, you can’t help but bookmark every R-related programming book you find in the hopes that one day you, or someone you know, might find it useful.

Hopefully this is the only bookmark you’ll need in future ;).

When I initially released this collection in late August 2020, it contained about 100 books that I’d been collecting over the previous two years. Since then I’ve found a few more and there have been contributions from many people. The collection now stands at about 250 books.

Most of these are free. Some are paid but usually quite affordable.