<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/should-i-learn-sf-or-sp-for-spatial-r-programming/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.seascapemodels.org/rstats/2018/03/23/should-I-learn-sp-or-sf.html"> Bluecology blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Should I learn sf or sp for spatial R programming?</h1>
<p>I recently received an email about some of the <a rel="nofollow" href="http://www.seascapemodels.org/code.html#" target="_blank">short-courses</a> on my webpage. The correspondant was asked whether my <a rel="nofollow" href="http://www.seascapemodels.org/rstats/rspatial/2015/06/22/R_Spatial_course.html" target="_blank">course on Maps and GIS in R</a> was still relevant, it being written in 2014.</p>
<p>The R environment is changing so fast, it is worth asking whether course material that is a few years old is still relevant.</p>
<p>Much of my 2014 course still remains relevant, like the aspects about spatial projections, raster package. The material about points and polygons is still relevant, but may not be soon. Finally, there are some things the course misses that I would include now, like integration with leaflet maps (which is in some of my later courses).</p>
<p>Here are a few major changes in the R environment for spatial analysis as I see it, and some important trends.</p>
<h2>Spatial data-structures and the simple features package</h2>
<p>The <code>sp</code> package for R provides spatial data-structures (termed ‘classes’) and a few utility functions (like <code>spplot</code> for plotting). In the past having this package has been essential for just about any spatial analysis in R, whether it was GIS or spatial modelling.</p>
<p>A newish package on the block <code>sf</code> has much the same functionality as <code>sp</code>, but promises to be much more convenient and flexible. It has been around a few years (I think), but there was little information about it. The release of the book <a rel="nofollow" href="https://geocompr.robinlovelace.net/" target="_blank">Geocomputation with R</a> and an increasing number of solutions on stackoverlow will probably see an many more people using <code>sf</code>.</p>
<p><code>sf</code> works with a language independent standard for structuring spatial data termed ‘simple features’. Just a few of the many nice aspects of <code>sf</code> are:</p>
<p><strong>Intuitive data structures</strong> <code>sp</code> data, like the <code>SpatialPolygonsDataframe</code> were composed of lists inside lists and were quite hard to decompose (there was a good reason why it was coded like this, but I forget why). <code>sf</code> objects are just data-frames that are collections of spatial objects. Each row is a spatial object (e.g. a polgyon), that may have data associated with it (e.g. its area) and a special geo variable that contains the coordinates.</p>
<p><strong>Intuitive operations</strong> One thing that frustrated me about spatial operations (like intersection) with <code>sp</code> and <code>rgeos</code> (the package for geographic operations), was that a dataframe <code>sp</code> object changed to a different, data-less, class when the operation was performed. <code>sf</code> objects don’t change class when you apply spatial operations to them (and they keep their associated data).</p>
<p><strong>Spatial indexing</strong> which can massively speed up spatial queries, like intersecting polygons, <a rel="nofollow" href="https://www.r-spatial.org/r/2017/06/22/spatial-index.html" target="_blank">especially on large datasets</a>.</p>
<h2>So should I learn sf or sp now?</h2>
<p>That’s a tough question. If you have time, I would say, learn to use both. <code>sf</code> is pretty new, so a lot of packages that depend on spatial classes still rely on <code>sp</code>. So you will need to know <code>sp</code> if you want to do any integration with many other packages, including <code>raster</code> (as of March 2018).</p>
<p>However, in the future we should see an increasing shift toward the <code>sf</code> package and greater use of <code>sf</code> classes in other packages. I also think that <code>sf</code> is easier to learn to use than <code>sp</code>.</p>
<p>Currently I use a hybrid of <code>sf</code> and <code>sp</code>, which has not been too hard to get my head around. I do most spatial operations in <code>sf</code>, and convert to <code>sp</code> objects only when I have to, such as if I wanted to <code>rasterize</code> a polygon object.</p>
<h2>What was missing in the 2014 course?</h2>
<p>A major trend in R packages now is integration with other platforms.</p>
<p>This is exemplified by the <a rel="nofollow" href="https://rstudio.github.io/leaflet/" target="_blank"><code>leaflet</code> package</a>, which allows one to create interactive web maps via R. I think teaching some of this stuff is essential now, even in begginer courses (it is pretty easy to make these maps). In fact, I do teach it now, even in general R courses, like this one for <a rel="nofollow" href="http://www.seascapemodels.org/data/Conservation_R.html" target="_blank">conservation biologists</a>.</p>
<p>Creating interactive maps is a lot of fun and potentially very useful. For instance see Ross Dwyer’s maps that let you explore the <a rel="nofollow" href="https://rossdwyer.shinyapps.io/sharkray_mpa/" target="_blank">distributions of sharks and rays around the world</a> (this one may take a moment to load, as the data-sets are large).</p>
<p>So in the future GIS and spatial analysis in R should become more straightfoward and you should be able to smoothly integrate R’s outputs with many other programs.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.seascapemodels.org/rstats/2018/03/23/should-I-learn-sp-or-sf.html"> Bluecology blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/IAC8Z_COlGE" height="1"></a></div>