Just to quickly share this great new website where you can search the documentation of all R packages and functions available on CRAN (6688 R packages and 136342 R functions at the moment of writing!). It offers instant or advanced search options in an easy interface, and there is even a package that allows you to use the search functionality from within R. Check it out on http://www.rdocumentation.org/.

# Tag Archives: R

# Import GRASS function console output as data.frame in R

In R you can use system calls or the spgrass6 package to run GRASS GIS functions. To do this, you need to run R from within GRASS GIS. This is as simple as starting GRASS GIS and subsequently starting R from the command line. See the GRASS-wiki for a more detailed background.

**The issue at hand**

One of the user-cases is when you want to (1) run a GRASS function on e.g., a raster layer and (2) capture the console output in a R data frame. For example, you can run the following in R:

MyVariables <- execGRASS("r.stats", flags="c", input="MyMap", separator=",", intern=TRUE)

However, the output is not in a very convenient format. Continue reading

# Calculating the raster cell area of an unprojected raster layer

What if you get a raster layer with number of people per raster cell, like for example the population layer from Afripop, and you want to convert it to a population density layer?

Well, obviously, you need to divide the number of people by the surface area of the raster cells. However, the surface area of the raster cells of an unprojected (lat/lon) are not constant; they decrease with increasing latitude. So what you need is a raster layer with the surface areas of the cells.

I thought I had seen a function in GRASS GIS to do this, but that might have been a typical case of the wish being the father to the thought. But anyway, it isn’t terribly difficult to calculate it yourself using the map calculator. Continue reading

# Multivariate Environmental Similarity Surfaces (MESS) index in GRASS GIS

The Multivariate Environmental Similarity Surfaces (MESS) is an index that represents how similar a point in space is to a reference set of points, with respect to a set of predictor variables (Elith et al 2010). The function was first implemented as part of the Maxent software package, and is also available in the dismo package for R (see also here and here).

The latter works well on small and medium sized data sets. However, they take a long time to run on larger data sets, e.g., when working with 1km² raster grids covering eastern Africa. I therefore wrote a small R script to compute MESS in GRASS GIS. Continue reading

# Reading GRASS GIS vector attribute tables into R

Linking GRASS GIS and R will give you a very powerful set of geo-spatial analytical tools. The spgrass6 offers a very convenient interface between GRASS GIS and R. You can read more about this package in Bivand, R. 2007. Using the R-GRASS interface. OSGeo Journal 1, 36-38.

**Read the whole vector layer**

It allows you amongst others to easily import vector data layers from GRASS GIS into R using the function `readVECT6()`

. This will import the whole vector layer. But what if you you only need to import the attribute table? Importing the whole vector layer would give unnecessarily overhead and would take (much) longer to import. Continue reading

# A faster way to calculate MESS in R – or – a tribute to Stack overflow

Jean-Pierre Rossi introduced a function to calculate the “Multivariate Environmental Similarity Surfaces” in R. Since then, the function has become part of the dismo package, which is a package maintained by Robert J. Hijmans and which offers a whole lot of functions for species distribution modelling.

The function calculates the MESS based on a point layer (the reference points) and a set of raster layers (the environmental data layers). It works great for smaller data sets, but you may run into trouble for very large data sets. So I needed to find a way to run this faster. And… I couldn’t. It is already a pretty neat piece of code, and I could not think of a better way of doing this in R. (~~I am working on~~ For an alternative to do this in GRASS, ~~but more about that later:~~ see here).

That is where I decided to try my luck on Stack Overflow. Continue reading

# Integrating Maxent, R and GRASS GIS

The maximum-entropy (Maxent) methods is one of the most widely used approaches for species habitat modelling. It has its own dedicated software, the Maxent software (written in java and therefore cross-platform). The software is easy to use and includes fairly a complete help file and tutorial. But things get better… Continue reading

# Better winGRASS 7 with R-integration

If you are a Window user and are using or want to use R and GRASS in combination, you will be happy to by this announcement on the GFOSS blog that there is now a better integration of WinGRASS 7 and R.

This integration is available for Linux users for a very long time. It was actually one of the principle reasons I started to use Linux. But now Window users can enjoy the experience too :-). Further user instructions are given on the GRASS wiki.

# A short note on the use of predict with the dismo or raster package

R has some great packages for species distribution modelling. One of these packages is the dismo package.

Models objects created with one of the various distribution models available in dismo can be used to make prediction for any combination of values of the independent variables. To do this, you use the ‘*predict*‘ function. The predict function requires a model object and a RasterStack or dataframe with the independent variables.

So, what will be faster, a RasterStack or dataframe as input in the predict function? Continue reading

# Cross tables in R, some ways to do it faster

In R to create a contingency table of the counts of the combination of two variables, I would normally resort to table(). But how fast is it? A question that becomes more relevant when working on large tables and when you have to run it very often. As you will see in the examples below, it isn’t terribly fast and there are other ways to create cross tables faster. Continue reading