Tag Archives: rstats

From a long to a wide R data frame, with array values

From a “long” data frame with columns containing the names of measured properties and corresponding vectors of values, attempting to make a “wide” data frame with a column for each measured property. Appears as though the cast() function of the reshape … Continue reading

Posted in data munging, R | Tagged , , , , ,

Storing numeric vectors (or arrays) in fields of an R data frame

To store variable-length data vectors and corresponding single-value data in separate fields of the same row of an R data frame, add a new modeless column then populate it: R recognizes that the vector’s class is numeric, but it is necessary … Continue reading

Posted in data munging, R | Tagged , , , ,

rgdal on Mac OS X

This was a bit of a headache to get right. Here’s what worked on OS X 10.6.7 (Snow Leopard, i386 architecture, R version 2.12.2): 1) Download the “GDAL Complete” framework from http://www.kyngchaos.com/software/frameworks and install. 2) Open R, and on the startup panel in … Continue reading

Posted in os x, packages, R | Tagged , ,

Tell it it’s a data frame: reprise

Having previously applied the function gsub() over an R data frame, I found that I not only want a data frame back but also one that has both numeric and character columns (in this case, converting some character to numeric.) … Continue reading

Posted in data munging, R | Tagged ,

Use do.call() instead of a loop when appending R data frames

Tip: When appending multiple data frames together, use do.call() instead of looping. The trick is to store all of the data frames within a list object: Difference in runtime is dramatic. N.B. the data frames must all have the same … Continue reading

Posted in data munging, R | Tagged ,

Tell it it’s a data frame, or: How to apply a function over an entire R data frame and get a data frame back

The “apply” family of functions are very useful (and usually fast), but when running a function over an entire data frame a data frame is not returned. To get one, just tell the result it is one:

Posted in data munging, R | Tagged , , , ,

Add an empty row and column to an R data frame

Several years ago I was stumped trying to figure out how to create an empty R data frame and populate its rows and columns with irregular data slurped in using scan(). My solution was pretty ugly, involving a lot of … Continue reading

Posted in data munging, R | Tagged , , , ,