Category Archives: data munging

Getting names of objects in an R workspace by type

To identify data frames in a workspace, and iterate over them by name, use Filter() with a few other functions: Here, objects() lists objects in the workspace, class() identifies their classes, and sapply() looks at each object in turn, returning … Continue reading

Posted in data munging, R | Tagged

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 , , , ,

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 , , , ,