Hello R lovers, I have a vector of dates and signals. I want to filter the signals per day in a way that only the first signal of the day remains like this: Dates Signals Filtered Signal 2006-11-02 0 0 2006-11-02 1 1 2006-11-02 0 0 2006-11-02 1 0 2006-11-02 1 0 2006-11-02 0 0 2006-11-03 1 1 2006-11-03 1 0 2006-11-03 0 0 2006-11-03 1 0 2006-11-03 0 0 The thing is that I want to do it with matrix functions and not doing a loop but I still have no clue even after having spent time on it. Can somebody please help me? thank you! ----- Anna Lippel -- View this message in context: http://n4.nabble.com/filtering-signals-per-day-tp1577044p1577044.html Sent from the R help mailing list archive at Nabble.com.
May doing a cumsum restricted on each date could help, I remember I read someone doing this on a post...because once i do this i will be able to discriminate the numbers different from 1. ----- Anna Lippel -- View this message in context: http://n4.nabble.com/filtering-signals-per-day-tp1577044p1577048.html Sent from the R help mailing list archive at Nabble.com.
ifelse(ave(signals, dates , FUN=cumsum) = 1, 1, 0) ----- Anna Lippel -- View this message in context: http://n4.nabble.com/filtering-signals-per-day-tp1577044p1577102.html Sent from the R help mailing list archive at Nabble.com.
Try this: f$Filtered <- unlist(sapply(unstack(f, Signals ~ Dates), function(.x)tabulate(which(as.logical(.x))[1], length(.x)))) On Wed, Mar 3, 2010 at 4:20 PM, anna <lippelanna24 at hotmail.com> wrote:> > Hello R lovers, > I have a vector of dates and signals. I want to filter the signals per day > in a way that only the first signal of the day remains like this: > Dates ? ? ? ? ? ? ? Signals ? ? ? Filtered Signal > 2006-11-02 ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-02 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?1 > 2006-11-02 ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-02 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-02 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-02 ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-03 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?1 > 2006-11-03 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-03 ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-03 ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?0 > 2006-11-03 ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ?0 > > The thing is that I want to do it with matrix functions and not doing a loop > but I still have no clue even after having spent time on it. Can somebody > please help me? thank you! > > > > ----- > Anna Lippel > -- > View this message in context: http://n4.nabble.com/filtering-signals-per-day-tp1577044p1577044.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Tena koe Anna
The following appears to do what you want (anna is a dataframe
containing the data you provided):
anna$Test <- anna$Signals
anna[duplicated(paste(anna$Dates, anna$Signals)),'Test'] <- 0
anna
Dates Signals Filtered.Signal Test
1 02-11-06 0 0 0
2 02-11-06 1 1 1
3 02-11-06 0 0 0
4 02-11-06 1 0 0
5 02-11-06 1 0 0
6 02-11-06 0 0 0
7 03-11-06 1 1 1
8 03-11-06 1 0 0
9 03-11-06 0 0 0
10 03-11-06 1 0 0
11 03-11-06 0 0 0
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of anna
> Sent: Thursday, 4 March 2010 8:20 a.m.
> To: r-help at r-project.org
> Subject: [R] filtering signals per day
>
>
> Hello R lovers,
> I have a vector of dates and signals. I want to filter the
> signals per day in a way that only the first signal of the
> day remains like this:
> Dates Signals Filtered Signal
> 2006-11-02 0 0
> 2006-11-02 1 1
> 2006-11-02 0 0
> 2006-11-02 1 0
> 2006-11-02 1 0
> 2006-11-02 0 0
> 2006-11-03 1 1
> 2006-11-03 1 0
> 2006-11-03 0 0
> 2006-11-03 1 0
> 2006-11-03 0 0
>
> The thing is that I want to do it with matrix functions and
> not doing a loop but I still have no clue even after having
> spent time on it. Can somebody please help me? thank you!
>
>
>
> -----
> Anna Lippel
> --
> View this message in context:
> http://n4.nabble.com/filtering-signals-per-day-tp1577044p1577044.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>