Posts Tagged ‘ function ’

Clarke and Ainsworth’s BIOENV and BVSTEP (and BIO-BIO etc…)

June 30, 2011
By
Clarke and Ainsworth’s BIOENV and BVSTEP (and BIO-BIO etc…)

Nonmetric Multidimensional Scaling (NMDS) plot of vegetation sample dissimilarities with best correlating environmental variables (left) and species (right) plotted as vectors (datasets "varespec" and "varechem" from the package ...

Read more »

Image color palette replacement

June 9, 2011
By
Image color palette replacement

Here is an example of a function I wrote to change the color palette used in an image. The above example comes from a black and white original, although color images can also be used. The function first converts the image to grayscale in order to have ...

Read more »

map.xyz(): interpolation of XYZ data and projection onto a map

May 30, 2011
By
map.xyz(): interpolation of XYZ data and projection onto a map

     I am still struggling to get a grasp of R's mapping capabilities. Part of my frustration lies in the fact that I often work on areas near the poles, which complicates interpolation across the 180 degree line. For smaller areas, interpolati...

Read more »

Array position to matrix coordinates conversion

May 30, 2011
By
Array position to matrix coordinates conversion

#A function that is sometimes useful in determining the #coordinate(i.e. row and column number) of a matrix position#(and vice-versa). #Either a vector of positions ("pos") #OR a 2 column matrix of matrix coordinates, ("coord", i.e. cbind(row,col)), #A...

Read more »

R functions for Earth geographic coordinate calculations

May 29, 2011
By
R functions for Earth geographic coordinate calculations

Here are some functions that I regularly use for geographic data (e.g. binning, filtering, calculation of new positions etc.).#distance in kilometers between two long/lat positions (from "fossil" package)earth.dist <- function (long1, lat1, long2, l...

Read more »

Example 8.35: Grab true (not pseudo) random numbers; passing API URLs to functions or macros

April 19, 2011
By
Example 8.35: Grab true (not pseudo) random numbers; passing API URLs to functions or macros

Usually, we're content to use a pseudo-random number generator. But sometimes we may want numbers that are actually random-- an example might be for randomizing treatment status in a randomized controlled trial.The site Random.org provides truly rando...

Read more »

Programming with R – Checking Data Types

November 13, 2010
By

There are a number of useful functions in R that test the variable type or convert between different variable types. These can be used to validate function input to ensure that sensible answers are returned from a function or to ensure that the function doesn’t fail. Following on from a previous post on a simple function

Read more »

Programming with R – Returning Information as a List

November 1, 2010
By

In previous posts (here and here) we created a simple function that returns a single numeric value. In some situations it may be more useful to return a more flexible data type, such as a list object, to provide more information about the calculations that have been performed. We can extend our previous function by changing

Read more »

Programming with R – Checking Function Arguments

October 24, 2010
By

In a previous post we considered writing a simple function to calculate the volume of a cylinder by specifying the height and radius of the cylinder. The function did not have any checking of the validity of the function arguments which we will consider in this post. R has various functions that we can use to

Read more »

Programming with R – Function Basics

October 20, 2010
By

One of the benefits of using R for statistical analysis is the programming language which allows users to define their own functions, which is particularly useful for analysis that needs to be repeated. For example, a monthly output from a database may be provided in a pre-determined format and we might be interested in running

Read more »