R-Bloggers

Function for Generating LaTeX Tables with Decimal Aligned Numbers

The xtable package is tremendously useful for generating LaTeX tables from data frames. It is also pretty easy to customize the output to handle some special cases of LaTeX formatting. The xtable.decimal function will create a LaTeX table where numeric columns will be vertically aligned on the decimal point. In addition to specifying the LaTeX alignment code it will also create appropriate column titles so that the column name spans the two resulting columns.

Using (R) Markdown, Jekyll, & Github for a Website

Introduction Markdown has been growing in popularity for writing documents on the web. With the introduction of R Markdown (see also Jeromy Anglim’s post on getting started with R Markdown) and knitr, R Markdown has simplified the publishing of R analysis on the web. I recently converted my website from Wordpress to Jekyll. Jekyll is a “static site generator” and is the framework used by GitHub Pages. You can view the complete source for this website on Github at https://github.

Fifty Shades of Grey in R

My wife went out to her book group tonight and their book of the month was 50 Shades of Grey. Sadly, I could think of is that plotting 50 shades in R would be a neat exercise. require(ggplot2) grey50 <- data.frame( x = rep(1:10, 5), y = rep(1:5, each=10), c = unlist(lapply(seq(10,255,5), FUN=function(x) { rgb(x,x,x, max=255) })), t = unlist(lapply(seq(10,255,5), FUN=function(x) { ifelse(x > 2552, ‘black’, ‘white’) })) ) ggplot(grey50, aes(x=x, y=y, fill=c, label=c, color=t)) + geom_tile() + geom_text(size=4) + scale_fill_identity() + scale_color_identity() + ylab(NULL) + xlab(NULL) + theme(axis.

Fun with coin flips

We all know that the odds of flipping an unbiased coin is 50% heads, 50% tails. But what happens if you do this a lot of times. Do you expect the same number of heads and tails? What if we took a cumulative sum where heads = +1 and tails = -1. What would that sum be? Here is a function that will do this n times and plot it.

Visualizing Missing Data

There are several graphics available for visualizing missing data including the VIM package. However, I wanted a plot specifically for looking at the nature of missingness across variables and a clustering variable of interest to support data preparation in multilevel propensity score models (see the multilevelPSA package). The following examples uses data from the Programme of International Student Assessment (PISA; see pisa package). The required packages can be downloaded from github.

User Input using tcl/tk

I was inspired by Kay Cichini recent post on creating a a tcl/tk dialog box for users to enter variable values. I am going to have a use for this very soon so took some time to make it a bit more generic. What I wanted is a function that takes a vector (of variable names) of arbitrary length, create a dialog box for an input for each, and return the values in a list.

Graphic Parameters (symbols, line types, and colors) for ggplot2

Following up on John Mount’s post on remembering symbol parameters in ggplot2, I decided to give it a try and included symbols, line types, and colors (based upon Earl Glynn’s wonderful color chart). Code follows below. require(ggplot2) require(grid) theme_update(panel.background=theme_blank(), panel.grid.major=theme_blank(), panel.border=theme_blank()) #Borrowed (i.e. stollen) from http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.R getColorHexAndDecimal <- function(color) { if(is.na(color)) { return(NA) } else { c <- col2rgb(color) return(sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3])) } } Symbols ggplot(data=data.

Given a room with n people in it, what is the probability any two will have the same birthday?

Revisiting a fun puzzle I remember first encountering as an undergraduate. Nice example of creating a plot in R using ggplot2. I also plot the probability of someone in the room having the same birthday as you.

See http://en.wikipedia.org/wiki/Birthday_problem for an explanation of the problem require(ggplot2) require(reshape) theme_update(panel.background=theme_blank(), panel.grid.major=theme_blank(), panel.border=theme_blank()) birthday <- function(n) { 1 - exp( - n^2 / (2 * 365) ) } myBirthday <- function(n) { 1 - ( (365 - 1) / 365 ) ^ n } d = 200 df = data.

Object Oriented Programming in R

Constructor Define S3 generic method for the print function. Test code As someone who was a Java programmer for many years learning R’s object oriented programming framework has been frustrating to say the least. I like the simplicity of S3 but find it limiting when you wish to write methods that change the underlying data elements.

Retrieving RSS Feeds Using Google Reader

I have been working on a new package makeR to help manage Sweave projects where you wish to create multiple versions of documents that are based on a single source. For example, I create lots of monthly and quarterly reports using Sweave and the only differences between versions are a few variables. I have used GNU make and Apache ANT but wanted a 100% R solution. I will have more to write about that project in a few weeks.