Hi You will get only general answer without some example data. Se Posting Guide.
If I understand correctly you need to reshape your df to have a structure unixtime, valuefactor1, valuefactor2 and after that valuefactor1-valuefactor2 shall give you desired solution. One possible way is to split your data frame to two e.g. df1 <- df[df$factor1=="bla",] df2 <- df[df$factor1!="bla",] and then merge df.m <- merge(df1, df2, by="unixtime", all=TRUE) Regards Petr > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Zhang Weiwu > Sent: Thursday, August 15, 2013 6:31 PM > To: r-help@r-project.org > Subject: [R] to match samples by minute > > > Perhaps this is simple and common, but it took me quite a while to > admit I cannot solve it in a simple way. > > The data frame `df` has the following columns: > > unixtime, value, factor > > Now I need a matrix of: > > unixtime, value-difference-between-factor1-and-factor2 > > The naive solution is: > > df[df$factor == "factor1",] - df[df$factor == "factor2",] > > It won't work, because factor1 has 1000 valid samples, factor2 has 1400 > valid samples. The invalid samples are dropped on-site, i.e. removed > before piped into R. > > To solve it, I got 2 ideas. > > 1. create a new data.frame with 24*60 records, each record represent a > minute in the day, because sampling is done once per minute. Now fit > all records into their 'slots' by their nearest minute. > > 2. pair each record with another that has similar unixtime but > different factor. > > Both ideas require for loop into individual records. It feels to C-like > to write a program that way. Is there a professional way to do it in R? > If not, I'd even prefer to rewrite the sampler (in C) to not to discard > invalid samples on-site, than to mangle R. > > Thanks. > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.