Publications by Forester
Convert polar coordinates to Cartesian
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 point (known location) to the unknown location.polar2cart<-function(x,y,dist,bearing...
714 sym R (649 sym/1 pcs)
Approximate sunrise and sunset times
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<-function(d,Lat=48.1442,Long=-122.7551){ ## d is the day of year ## Lat is latitude...
652 sym R (1604 sym/1 pcs)
Preparing plots for publication
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 tweaking layout parameters tends to run out quickly. I searched around and found a n...
1193 sym R (182 sym/2 pcs)
Convert factors to numbers
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<-factor(c(round(rnorm(10),2),"A","B",NA)) > 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.2...
675 sym R (416 sym/1 pcs)
Drop unused factor levels
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 this post to R-Help. Here is an example:> a <- factor(letters) > ...
1179 sym R (559 sym/3 pcs)
Plotting contours
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 the density of x and y; however, if you set plot.dens=FALSE, the contours will be added to the...
901 sym R (2941 sym/1 pcs) 2 img
Error capture
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<-try(lm(y~x,dataset)) results<- ifelse(class(fit)=="try-error", NA, fit$coefficients) } Related To leave a comment for the author, please follow the link and comment on their blog: Quantitative Ecology. R-b...
518 sym R (111 sym/1 pcs)
Call C from R and R from C
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 an incredibly clear treatment of Baye...
2117 sym R (2531 sym/5 pcs)
R matrices in C functions
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 using the following C functions (idea from here):int Cmatrix(int *row, int *col, in...
717 sym R (559 sym/1 pcs)
Truncated Normal Distribution
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 sometimes preferred, and the truncated normal is particularly popular. While R has a pa...
1065 sym R (843 sym/1 pcs)