Articles by Mollie

Merge by City and State in R

February 20, 2014 | Mollie

Often, you'll need to merge two data frames based on multiple variables. For this example, we'll use the common case of needing to merge by city and state.First, you need to read in both your data sets:# import city coordinate data:coords [Read more...]

Check if a Variable Exists in R

December 5, 2013 | Mollie

If you use attach, it is easy to tell if a variable exists. You can simply use exists to check:__attach(df)__exists("varName")[1] TRUEHowever, if you don't use attach (and I find you generally don't want to), this simple solution doesn't ... [Read more...]

Custom Legend in R

October 10, 2013 | Mollie

This particular custom legend was designed with three purposes: To effectively bin values based on a theoretical minimum and maximum value for that variable (e.g. -1 and 1 or 0 and 100) To use a different interval notation than the default To handle NA values Even though this particular legend was designed ... [Read more...]

Perform a Function on Each File in R

September 26, 2013 | Mollie

Sometimes you might have several data files and want to use R to perform the same function across all of them. Or maybe you have multiple files and want to systematically combine them into one file without having to open each file and manually copy the... [Read more...]

Truncate by Delimiter in R

September 19, 2013 | Mollie

Sometimes, you only need to analyze part of the data stored as a vector. In this example, there is a list of patents. Each patent has been assigned to one or more patent classes. Let's say that we want to analyze the dataset based on only the first pat... [Read more...]

Only Load Data If Not Already Open in R

September 12, 2013 | Mollie

I often find it beneficial to check to see whether or not a dataset is already loaded into R at the beginning of a file. This is particularly helpful when I'm dealing with a large file that I don't want to load repeatedly, and when I might be using the... [Read more...]

Using colClasses to Load Data More Quickly in R

September 5, 2013 | Mollie

Specifying a colClasses argument to read.table or read.csv can save time on importing data, while also saving steps to specify classes for each variable later.For example, loading a 893 MB took 441 seconds to load when not using colClasses, b... [Read more...]

Plot Weekly or Monthly Totals in R

August 29, 2013 | Mollie

When plotting time series data, you might want to bin the values so that each data point corresponds to the sum for a given month or week. This post will show an easy way to use cut and ggplot2's stat_summary to plot month totals in R wi... [Read more...]

Date Formats in R

August 22, 2013 | Mollie

Importing DatesDates can be imported from character, numeric, POSIXlt, and POSIXct formats using the as.Date function from the base package.If your data were exported from Excel, they will possibly be in numeric format. Otherwise, they will m... [Read more...]

geom_point Legend with Custom Colors in ggplot

March 7, 2013 | Mollie

Formerly, I showed how to make line segments using ggplot.Working from that previous example, there are only a few things we need to change to add custom colors to our plot and legend in ggplot.First, we'll add the colors of our choice. I'll do th... [Read more...]

Shapefiles in R

February 28, 2013 | Mollie

Let's learn how to use Shapefiles in R. This will allow us to map data for complicated areas or jurisdictions like zipcodes or school districts. For the United States, many shapefiles are available from the Census Bureau. Our example will map U.S. nati... [Read more...]

Elevation Profiles in R

February 21, 2013 | Mollie

First, let's load up our data. The data are available in a gist. You can convert your own GPS data to .csv by following the instructions here, using gpsbabel.gps [Read more...]

GPS Basemaps in R Using get_map

February 14, 2013 | Mollie

There are many different maps you can use for a background map for your gps or other latitude/longitude data (i.e. any time you're using geom_path, geom_segment, or geom_point.)get_mapHelpfully, there's just one function that will allow you to query Google Maps, OpenStreetMap, Stamen maps, ... [Read more...]

Using Line Segments to Compare Values in R

January 31, 2013 | Mollie

Sometimes you want to create a graph that will allow the viewer to see in one glance:The original value of a variableThe new value of the variableThe change between old and newOne method I like to use to do this is using geom_segment and geom_poin... [Read more...]

Storing a Function in a Separate File in R

January 24, 2013 | Mollie

If you're going to be using a function across several different R files, you might want to store the function in its own file.If you want to name the function in its own fileThis is probably the best option in general, if only because you may want to p... [Read more...]

Stacked Bar Charts in R

January 10, 2013 | Mollie

Reshape Wide to LongLet's use the Loblolly dataset from the datasets package. These data track the growth of some loblolly pine trees.__ Loblolly[1:10,]   height age Seed1    4.51   3  30115  10.89   ... [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)