Tao of for() in R

Tao of for() in R

Think of dimensions as data

So, for loops are dead simple (as long as you remember to get the results out of the .Local environment into the .Global environment).

v <- vector(length = length(object)) for(i in seq_along(object)) v[i] = sqrt(i)

Therefore, substitute any f for sqrt and no worries. We know how to do it.

Except, of course, how may not produce exactly what we want.

For the past couple of weeks, a 2e6x1e3 object and I have been wrestling in aid of producing a 2e6x1e1 object of summary statistics in aid of an ecological time series.

My difficulty turned out to be that some of the summary statistics were derivable from the object, as a whole, while others had to be derived on a rowwise basis.

This seemed a good occasion for data.table. Wicked fast, subsetable and lots of other things to love. But, while any single row of the object worked fine with a little tweeking, the for loop failed consistently, at one step or another.

An internal dim light flickers: “It’s all integers, why a data table?” The first what question: Is data.table the right object? It’s lists, after all. That’s how you get characters to peacefully coexist with numerics in the same object. My object is all integers? Occum’s Razor applies. Work with a matrix, not a data.table, as lovely as it might be.

So, back to my f(x) = y. My x is now a matrix object, and my y can be, too, since the summary statistics are all numeric. So, initialize y

M <- matrix(nrow = x, nrow = y)

where

x = dim(object)[1] y = number_of_summary_statistics

creates a matrix object filled with NA of sufficient size to hold the results.

To populate M with the statistics,

M[,1] = stat1(object) M[,2] = stat2(object)

where object is the complete dataset

and the loop to add stats that are rowwise looks like

for(i in piece) M[,3] = apply(piece,1,somefunction)

where piece is a row of object.

Or, if there is more than one somefunction, just put the apply expressions into a separate script and use source(script.R)

As ever, it’s just a matter of

*What do I have?
*What do I want?
*What function converts the one into the other?