# Blog Archives

## Drop unused factor levels

February 4, 2008
By

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: >...

## Convert factors to numbers

November 29, 2007
By

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.61 1.12 1.26 0.09 -0.13 0.16 -0.03 -0.1 0.09 -0.47 A ...

## Preparing plots for publication

November 15, 2007
By

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 ...

## Approximate sunrise and sunset times

October 17, 2007
By

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<-fun...

## Convert polar coordinates to Cartesian

October 14, 2007
By

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 ...

## Reorder factor levels

October 3, 2007
By

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...

## Extract objects from a list

September 5, 2007
By

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...

## Execute system commands within an R Script

August 23, 2007
By

To execute a system command from within an R script, just use system(). This is handy for zipping large output files on the fly.write.csv(mydat, "mydat.csv")system("gzip mydat.csv", wait=FALSE)

## Offset in glm ()

August 17, 2007
By

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.

## Including arguments in R CMD BATCH mode

August 16, 2007
By

When you have multiple computers or processors at your disposal and wish to run the same script with different arguments, use the following at the command line (here described for Linux; remove the linebreak, it is just there for display purposes):\$ R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)'test.R test.out & Where test.R is the R script file you wish...