“Thus organisms and environments are both causes and effects

in a coevolutionary process.”

—Richard C. Lewontin in The Triple Helix: Gene, Organism, and Environment.

Tuesday, April 2, 2019

Helpful tips for R

There are always new things to learn in R that totally change how you use it. Then there are those things that are super frustrating but the solutions elude you for years. Here is my list of those things.


  • Problem: Can't remember the syntax for basic functions in several useful packages
    • Solution: R cheatsheets [link]
  • Problem: Can't use dates like a number line
    • Solution: package "lubridate" (there's a cheatsheet for it, too)
  • Problem: Can't specify limits to date axis in ggplot.
    • Solution: use "scale_x_date" instead of "scale_x_continuous"
  • Problem: It takes so much time to constantly update figures and tables into my manuscript.
    • Solution: Rmarkdown [link]. It knits together your writing and your code so your figures and tables are automatically updated.
  • Problem: Plots in base don't have x and y axes touching.
    • Solution: Within the plot() command, use xaxs='i', yaxs='i'
  • Problem: It's hard to summarize a lot of data when you need it split up in certain groups.
    • Solution: function ddply(). You tell it what your groups are, then you can specify what functions you want it to perform on any other variable and it will do this and split it up by your groups into a nice dataframe.
  • Problem: I want to use many TRUE/FALSE criteria to select certain rows of data.
    • Solution: use the syntax %in% and then a vector of criteria to tell R you are looking for items that match anything in your vector. Example: plot.com[which(plot.com$plot%in%c('A03','A04','B03','B04','NC01','NC06')),]
      looks for all values of plot.com where plot is A03 or A04 or B03, etc. You can also specify a vector object with the criteria you're trying to match, such as plot.com[which(plot.com$plot%in%my.plot.criteria),] where my.plot.criteria is a vector with the plots you want to specify.

No comments: