[R] How can I map "by" results to original list of indices or first difference of column of data.frame with two factors?

Mikhail Titov mlt at gmx.us
Sun Mar 4 01:50:42 CET 2012


Hello!

I’m having stacked data in a data.frame with 2 factors, ordered POSIXct, and actual value as numeric (as if for lattice::xyplot).

I would like to calculate first difference using “diff” function within corresponding subsets/partitions. Since data.frame is organized by factors and has sorted dates, it seems like "by" is a good candidate for the job. However it returns just a dumb list of vectors.

It seems that I can use either expand.grid to remap results of "by" and hope that I won't mess up order, or I can use "unique(subset(x,select=c(foo,bar)))"

In overall it looks like quite many steps for such task not counting assignment of those differences back to original data.frame starting from 2nd position in each partition (as diff returns shorter vector).

Am I on the right track or is there an easier way to do that?

Mikhail



More information about the R-help mailing list