Articles by Forester

Recursively search for text in R scripts.

December 13, 2016 | Forester

Often I find myself needing to search for .R or or .Rmd files in which I have used a specific function. There are a variety of searches that you can do; however, I wanted something that would work recursively at the command line of a Linux or Mac termi... [Read more...]

Sample uniformly within a fixed radius.

May 16, 2014 | Forester

I was asked how to do this today and thought that I would share the answer: ## Sample points uniformly within a fixed radiusnrand=1000maxstep=10## Sample data ## NB: To get a truly uniform sample over the circle, you must ## sample the square of the distance and then transform back.tempdat [Read more...]

RStudio and X2Go

April 28, 2014 | Forester

After a recent system upgrade (to Linux Mint LMDE), I was no longer able to run RStudio through the remote desktop application X2Go. It turns out that this is due to a problem with the Qt libraries (see this website). As suggested here, I just deleted... [Read more...]

Quantitative Ecology 2010-11-10 14:56:00

November 10, 2010 | Forester

At last... I have been suffering with XEmacs displaying odd characters instead of the quotation marks that are used in R help files. This was driving me up the wall because it makes the files (and R output in general) very hard to read; however, I fina... [Read more...]

ISO week

October 22, 2009 | Forester

I am working with a model that produces estimates of snow water equivalent through time. Because I deal with large spatial extents, I decided to have the model produce weekly averages. The problem with this is knowing which file to access for a given d... [Read more...]

Leap years

October 22, 2009 | Forester

A quick function that when provided a numeric vector of years returns a boolean vector where TRUE == Leap year.is.leapyear=function(year){ #http://en.wikipedia.org/wiki/Leap_year return(((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0))} [Read more...]

Truncated Normal Distribution

September 3, 2009 | Forester

Many distributions may be used to describe patterns that are non-negative; however, there are not as many choices when an upper bound is also needed (although the beta distribution is very flexible). For various reasons, truncated distributions are som... [Read more...]

R matrices in C functions

December 13, 2008 | Forester

Using the .C() function in R, you can only pass vectors. Since R stores matrices columnwise as vectors anyhow, they can be passed to your C function as vectors (along with the number of rows in the matrix) and then accessed in familiar [row,col] manner... [Read more...]

Call C from R and R from C

November 17, 2008 | Forester

Several years ago, while a research associate at the University of Chicago, I had the privilege of sitting in on a course taught by Peter Rossi: Bayesian Applications in Marketing and MicroEconometrics. This course -- one I recommend to anyone at U Chicago who is interested in statistics -- was ... [Read more...]

Error capture

May 6, 2008 | Forester

In a recent post to r-sig-ecology, Mike Colvin suggested the following to capture errors within a loop:for (i in 1:1000){fit [Read more...]

Plotting contours

March 18, 2008 | Forester

Plenty of packages allow you to plot contours of a "z" value; however, I wanted to be able to plot a specific density contour of a sample from a bivariate distribution over a plot that was a function of the x and y parameters. The example only plots th... [Read more...]

Drop unused factor levels

February 4, 2008 | Forester

When creating a subset of a dataframe, I often exclude rows based on the level of a factor. However, the "levels" of the factor remain intact. This is the intended behavior of R, but it can cause problems in some cases. I finally discovered how to clean up levels in ... [Read more...]

Convert factors to numbers

November 29, 2007 | Forester

If you have a vector of factors it is easy to get the factor level; however, I always forget how to extract the factor value. I ran into the answer here. __ x x [1] 1.61 1.12 1.26 0.09 -0.13 0.16 -0.03 -0.1 0.09 -0.47 [11] A B Levels: -0.03 0.09 -0.1 -0.13 0.16 -0.47 1.12 1.26 1.61 A B__ as.numeric(x) [1] 9 7 8 2 4 5 1 3 2 6 10 11 NA__ ... [Read more...]

Preparing plots for publication

November 15, 2007 | Forester

The plotting capabilities of R are excellent; however, when I am preparing a figure for publication, I often need to combine multiple plots or add objects (e.g., arrows or text) to an existing plot. While this can be accomplished in R, my patience for ... [Read more...]

Approximate sunrise and sunset times

October 17, 2007 | Forester

This function is not perfect, but it does a reasonable job estimating sunrise and sunset times for my field site. If more accurate data are required, try here. Note, the command to calculate day of year is: strptime(x, "%m/%d/%Y")$yday+1suncalc [Read more...]

Convert polar coordinates to Cartesian

October 14, 2007 | Forester

When I want to calculate the coordinates of a location (e.g., a nest or burrow) based on distance and bearing from a grid point, this function helps me avoid writing down SOH-CAH-TOA every time. Just note that the bearing in this case is from the grid ... [Read more...]

Reorder factor levels

October 3, 2007 | Forester

Very often, especially when plotting data, I need to reorder the levels of a factor because the default order is alphabetical. There must be many ways of reordering the levels; however, I always forget which package to look in. A direct way of reorderi... [Read more...]

Extract objects from a list

September 5, 2007 | Forester

When using Rmpi to send processes to many nodes, it is convenient to create a list of tasks that are assigned to nodes as they become available. In my case, I was working through a large factorial set of simulations and needed to use a unique set of va... [Read more...]

Offset in glm ()

August 17, 2007 | Forester

To add an offset to the linear predictor of a generalized linear model (or models from the survival package such as coxph and clogit), use offset(x) in the formula. This will add an offset to the linear predictor with known coefficient 1. [Read more...]
1 2

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)