[R] to match samples by minute

PIKAL Petr petr.pikal at precheza.cz
Fri Aug 16 09:36:33 CEST 2013


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-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Zhang Weiwu
> Sent: Thursday, August 15, 2013 6:31 PM
> To: r-help at 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 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.



More information about the R-help mailing list