(almost) rolling function or fill?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

(almost) rolling function or fill?

R help mailing list-2
Hello,
please see if you have a thought on how to achieve the following:
we have:
 df<-data.frame(a=Sys.Date()+1:10,               b=Sys.Date()+c(NA,NA,NA,rep(3,4),NA,NA,3),               c=Sys.Date()+c(NA,NA,NA,rep(9,4),NA,NA,9))


the idea I have difficulty wrapping my head around is to do the following: I need the system to look at df$a by row (lets call it the index row) and look at df$b and df$c 1 row before the given row in df$a  (lets call it index row -1) and evaluate if the index row value in df$a falls into the range (>= and <=) of the index row -1 values in df$b and df$c. If it does, then copy over the index row -1 values in df$b and df$c into the index row in df$b and df$c, if not place an NA in both cells of the index row in df$b and df$c. 
 examples:
1. the date value in df$a[8] is between df$b[7] and df$c[7] so we can copy the values in df$b[7] and df$c[7] into df$b[8] and df$c[8]2.  the date value in df$a[9] is between df$b[8] and df$c[8] (as we copied it in in step 1)  so we can copy the values in df$b[8] and df$c[8] into df$b[9] and df$c[9]3.  the date value in df$a[10] is NOT between df$b[9] and df$c[9] (as we copied it in in step 2)  so we can place NA in df$b[10] and df$c[10] 

also would like to do this going up, too, similar to fill(...,"downup"). On the end we would want to have this:
 dfwanted<-data.frame(a=Sys.Date()+1:10,               b=Sys.Date()+c(NA,NA,rep(3,7),NA),               c=Sys.Date()+c(NA,NA,rep(9,7),NA))


much appreciate any help you could provide.
thanks,

Andras 
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: (almost) rolling function or fill?

Bert Gunter-2
This is a plain text list. Your html post was completely mangled. Re-post
in plain text, please.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Jun 8, 2020 at 1:08 PM Andras Farkas via R-help <
[hidden email]> wrote:

> Hello,
> please see if you have a thought on how to achieve the following:
> we have:
>  df<-data.frame(a=Sys.Date()+1:10,
>  b=Sys.Date()+c(NA,NA,NA,rep(3,4),NA,NA,3),
>  c=Sys.Date()+c(NA,NA,NA,rep(9,4),NA,NA,9))
>
>
> the idea I have difficulty wrapping my head around is to do the following:
> I need the system to look at df$a by row (lets call it the index row) and
> look at df$b and df$c 1 row before the given row in df$a  (lets call it
> index row -1) and evaluate if the index row value in df$a falls into the
> range (>= and <=) of the index row -1 values in df$b and df$c. If it does,
> then copy over the index row -1 values in df$b and df$c into the index row
> in df$b and df$c, if not place an NA in both cells of the index row in df$b
> and df$c.
>  examples:
> 1. the date value in df$a[8] is between df$b[7] and df$c[7] so we can copy
> the values in df$b[7] and df$c[7] into df$b[8] and df$c[8]2.  the date
> value in df$a[9] is between df$b[8] and df$c[8] (as we copied it in in step
> 1)  so we can copy the values in df$b[8] and df$c[8] into df$b[9] and
> df$c[9]3.  the date value in df$a[10] is NOT between df$b[9] and df$c[9]
> (as we copied it in in step 2)  so we can place NA in df$b[10] and df$c[10]
>
> also would like to do this going up, too, similar to fill(...,"downup").
> On the end we would want to have this:
>  dfwanted<-data.frame(a=Sys.Date()+1:10,
>  b=Sys.Date()+c(NA,NA,rep(3,7),NA),
>  c=Sys.Date()+c(NA,NA,rep(9,7),NA))
>
>
> much appreciate any help you could provide.
> thanks,
>
> Andras
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.