Mikhail Titov
2012-Mar-04 00:50 UTC
[R] How can I map "by" results to original list of indices or first difference of column of data.frame with two factors?
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
jim holtman
2012-Mar-04 01:55 UTC
[R] How can I map "by" results to original list of indices or first difference of column of data.frame with two factors?
If you would post a subset of your data so that we can see what you are talking about, we could probably help you come up with a solution. On Sat, Mar 3, 2012 at 7:50 PM, Mikhail Titov <mlt at gmx.us> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Apparently Analagous Threads
- lattice: dots from xyplot to xscale.components
- lattice: How to get log base for each axis inside panel function?
- Parsing code with newlines
- Any existing functions for reading and extracting data from path names?
- RODBC: sqlUpdate doesn't handle properly POSIXct field?