# Articles by Tony Cookson

### Using apply() to create a unique id

February 6, 2012 |

Suppose you have a data set with two identifiers. For example, maybe you're studying the relationships among firms in an industry and you have a way to link the firms to one another. Each firm has an id, but the unique unit in your data set is a pair...

### Splitting Vectors of Uneven Strings

July 29, 2011 |

Suppose you have a vector of names such that the first three words in the vector contain relevant information, but there is a bunch of extraneous stuff. For example,Our goal is to collapse the first three words into one contiguous string (without the ...

### Avoiding Loops in R: An Example with Principal Minors

July 18, 2011 |

Yesterday, I found myself wanting to compute a large subset of the second order principal minors of a matrix (diagonal-preserving minors; the ones for which the rows and columns kept are the same). Don't judge me for wanting to do this, and bear with ...

### Today’s Assignment: Assignment

June 20, 2011 |

A new R user quickly discovers that there are multiple ways to store information into an object -- the technical term for this is assignment. There's = as in:x = c(1,2,3)and there's

### An application of aggregate() and merge()

June 5, 2011 |

Today, I encountered an interesting problem while processing a data set of mine. My data have observations on businesses that are repeated over time. My data set also contains information on longitude and latitude of the business location, but unfort...

### Recreational R: Simulating a Card Trick

May 30, 2011 |

In this post, I simulate an interesting card trick, which was described by a friend of mine named Xan. Here's Xan's description of the card trick:I put a deck of cards down face up on the table. Meanwhile you think of a secret number between 1 and 1...

### An Application of boot() to IV regression

May 28, 2011 |

Bootstrapping standard errors can be a useful technique when obtaining closed form for the standard error formula is difficult or intractable. In this post, I give an example of how to use R to create a bootstrap sampling distribution in the context o...

### Defaults, Lists and Classes: A Functional Post

May 20, 2011 |

In this post, I demonstrate a couple of useful tricks to writing functions in R. The context is a function I wrote called called samp() that allows for an easy demonstration of sampling distribution properties.DefaultsBy default, this function draws K...

### Porting Stata-like Marginal Effects to LaTeX

May 19, 2011 |

The TimeSeriesIreland Blog posted an excellent start on a function that automatically computes marginal effects for probit and logit models. As I have always wanted an easy-to-use function that computes and reports marginal effects in R, I was elated ...

### Describing Data: Frequently Used Commands

May 13, 2011 |

Obtaining a coherent numerical summary of data is a common task, and it is common to want to port these summary statistics into a table of results. When I am in interactive mode with my data, I use the summary() command applied to my data frame. For ...

### Extending mtable() to ivreg, gls and robust standard errors

May 9, 2011 |

I have written several extensions of the mtable() command in the memisc library that may come in handy. The methods are available in a package I have written called tonymisc (now, available on CRAN). A zipped folder with my package files is available...

### Putting Robust Standard Errors into LaTeX Tables: An Extension of mtable

May 3, 2011 |

I recently discovered the mtable() command in the memisc library and its use with toLatex() to produce nice summary output for lm and glm objects in a nicely formatted table like this:Once you have your linear model objects, all you need is one command...

### Forming Formulas

April 29, 2011 |

One of the first functions a new R user learns how to use is the lm() command, which involves stating the model formula.lm(y~x1+x2, data=mydata)After a while, this just becomes a natural way to say "I want a regression of y on x1 and x2 ...

### Automatically Save Your Plots to a Folder

April 26, 2011 |

Suppose you're working on a problem that involves a loop for calculations. At each iteration inside the loop, you want to construct a plot. Not only do you want to see the plot, but you would like to save each plot for a presentation, report or paper...

### R Bloggers

April 26, 2011 |

I recently found a great resource for R in the blogosphere, the R Bloggers Blog Aggregator. Basically, the site aggregates posts from a bunch of blogs about R (like this one!) into a giant feed of uses for R. If you are interested in learning more ab...

### Merging Data Video Tutorial

April 25, 2011 |

Here's a video tutorial where I walk through some code that does what the previous post describes.The FRED data is used extensively for macroeconomics. You might these data useful for joining in graph fights in the blogosphere.

### Merging Multiple Data Files into One Data Frame

April 24, 2011 |

We often encounter situations where we have data in multiple files, at different frequencies and on different subsets of observations, but we would like to match them to one another as completely and systematically as possible. In R, the merge() comma...

### Dates in R and the First Day of the Month

April 23, 2011 |

I spent some time this morning learning about how R thinks about dates in R. I found this website to be a useful guide.Imagine that your data are dates in a standard format [YEAR-MONTH-DAY (as in 2011-23-04) is one such format] and you want a vector o...

### Good Looking Maps in R

April 21, 2011 |

I haven't yet tried this, but it looks like a nice package.In one recent project I needed to draw several maps and visualize different kinds of geographical data on it. I found the combination of R/ggplot/maps package extremely flexible and powerful,...