Blog Archives

using C++ within R

June 8, 2014
By

Introduction Quite often I write which(...) to find the first element of a vector matching some condition. One has to wonder whether that’s wasteful, though, since there is no need to do any tests once one works. I decided to try using C++, using Rcpp, to see if speed advances could be made. Procedure 1 2 3 4 5 6 library(Rcpp) library(microbenchmark) cppFunction("\n ...

Read more »

slumping model

June 8, 2014
By
slumping model

Introduction I got interested in layered sedimentation from viewing a video and decided it would be interesting to code this into R. More on that in due course, but my first step was to code a syatem with one sediment “type”. Procedure The following code drops sediment particles at x=1, and lets them roll downhill until they reach the bottom or...

Read more »

colormap

April 30, 2014
By
colormap

Introduction Over the past month or so I have been trying different ways of handling GMT-style colormaps in Oce. I think my present solution is on the right track, but I am posting here to get more eyes on the problem. Note that the function called Colormap() here will be called colormap() if I decided to incorporate it into Oce....

Read more »

Using memoise to cache R values

April 12, 2014
By

Introduction The memoise package can be very handy for caching the results of slow calculations. In interactive work, the slowest calculations can be reading data, so that is demonstrated here. The microbenchmark package shows timing results. Methods and results Setup First, load the package being tested, and also a benchmarking package. 1 2 library(memoise) library(microbenchmark) Test conventional function The demonstration will be for reading a CTD file. 1 library(oce) ##...

Read more »

Sun and moon paths in daily sky

March 22, 2014
By
Sun and moon paths in daily sky

Introduction This blog started because I was interested in the sunrise position on the winter solstice of 2013. With the Spring equinox, I’m trying something different: plotting the paths of the sun and moon through the day. The code shown here produces a daily graph, and I have a cron job running on a machine so that this graph is...

Read more »

Box models in R

March 16, 2014
By
Box models in R

Introduction Box models are popular in some branches of oceanography and other geophysical disciplines, partly because they are simple to construct and to solve. This posting shows how to solve a box model in R, using the lsoda() function in the deSolve package. The basic ideas can be explained in the context of riverine input into a lake that connects...

Read more »

Digitizing plots

March 12, 2014
By
Digitizing plots

Introduction It is not uncommon to want to digitize values from a graph in a paper, whether to do some initial analysis without bothering an author, or to get data from a paper published so long ago that the data are available only graphically. Although there are several software applications that do this well, it is also possible to...

Read more »

Error bars in R

March 5, 2014
By
Error bars in R

Introduction Error propagation can be a fair bit of work with a calculator, but it’s easy with R. Just use R in repeated calculation with perturbed quantities, and inspect the range of results. Two methods are shown below for inspecting the range: sd() and quantile(), the latter using the fact that area under a normal distribution is 0.68...

Read more »

Oce translations

February 26, 2014
By
Oce translations

A new Oce user wondered how to get Spanish labels on axes. This is easy for some plot types because xlab and ylab arguments are obeyed, but in many cases the labels are generated by the code. That meant that it was finally time for me to start localizing Oce, using the GNU gettext scheme. ...

Read more »

Improved filtfilt() for R

February 19, 2014
By
Improved filtfilt() for R

The filtfilt function in R is supposedly based on the Matlab one, but it does quite badly on endpoints. My goal here is to explore alternatives, with a focus on the Octave method. NOTE: I may also try scipy for hints.) The best method of dealing with endpoints is still an open question. Matlab does one thing, Octave...

Read more »