Favorite Tricks

Top Ten Questions from New R Users

1.  How do I use probability distributions in R?  Answer from RWiki.

2.  How can I speed up loops in R?  An answer from R Revolutions.

3.  How do I subset data in R?  An answer from the UCLA introductory website.  Also check out these answers about sorting data and merging data on Quick-R.

4.  What is a good package to use to manipulate strings in R?  My favorite package is stringr for working with strings in R.

5.  What are the different types of data structures in R?  Find a great answer here.

6.  How do I customize graphical parameters in R?  Quick-R has a great description of fonts and colors, legends and labeling, and combining plots in R.

7.  How do I generate random numbers in R?  An answer from R Revolutions.

8.  How do I run regressions in R?  Check out this answer from Quick-R.

9.  How do I deal with dates in R?  An answer from the UCLA tutorial on R.

10.  How do I output a table from R to Latex?   The library to do this is xtable.  If you want to get fancy integrating R and Latex, check out Sweave. Or check out a new R package stargazer.

Fun things to do after you learn the basics:

Basic web-scraping in R:  Useful because sometimes you want to get data directly from a table on a website, but there is no way to download the current data into a readable file.  Also check out webscraping using R Curl and  readLines.

Tips for plotting maps in R.  And more on this.

TwitteR: scrape twitter using R!

Two ways to do genetic matching in R: This code snippet shows two ways of implementing genetic matching in R. The first method uses the GenMatch() package, and the second uses MatchIt(). Essentially, this is a template to show how you can set your covariates and extract the treated or control data. Note that with the MatchIt() package, you could implement various other matching methods by specifying e.g. "nearest" rather than "genetic."

Script for Reinstalling R Packages: Rich Nielsen's script to reinstall R packages after upgrade of R version. Also see this script for installing multiple R packages simultaneously.

Kountry: A R and Stata package for dealing with annoying country codes

Automatically create model formulas in R

R Benchmark: Package to help benchmark code in R.