1

I have a data frame that looks like this :

a b
7 -12
18 -22
29 -32
40 -42
51 -52

I want to slide from top to bottom with:

sliding window 3 :

7 -22 = -15

7 -32 = -25

18 -42 = -24

29 -52 = -23

40 -52 = -12

from this calculations I want R to report me the minimum which is -25.

for sliding window 5 :

7-32 =-25

7-42 = -35

7 -52 = -45

18 -52 =-34

29-52 =-23

and R to report me -45.

How can I do this in R ?

library(tidyverse)
a = c(7,18,29,40,51)
b = c(-12,-22,-32,-42,-52)
w = tibble(a,b);w
Homer Jay Simpson
  • 1,043
  • 6
  • 19

2 Answers2

3

For the first step

w = w |> 
  mutate(
    sw3 = lag(a, 1L, default = first(a))  + lead(b, 1L, default = last(b)),
    sw5 = lag(a, 2L, default = first(a))  + lead(b, 2L, default = last(b))  
  )
#       a     b   sw3   sw5
#   <dbl> <dbl> <dbl> <dbl>
# 1     7   -12   -15   -25
# 2    18   -22   -25   -35
# 3    29   -32   -24   -45
# 4    40   -42   -23   -34
# 5    51   -52   -12   -23

To find the lowest value:

lapply(select(w, sw3, sw5), min)
# $sw3
# [1] -25

# $sw5
# [1] -45

Or

map_dbl(select(w, sw3, sw5), min)
# sw3 sw5 
# -25 -45  
s_baldur
  • 29,441
  • 4
  • 36
  • 69
1

Try

library(dplyr)
df1 %>% 
   mutate(b3 = lead(b, default = last(b)), 
   a3= lag(a, default = first(a)), 
   b_a = b3 + a3, 
   b5 = lead(b, n = 2, default = last(b)),
   a5 = lag(a,n = 2, default = first(a)), b_a_5 = b5 + a5) %>%
   summarise(out1 = min(b_a), out2 = min(b_a_5))

-output

 out1 out2
1  -25  -45
akrun
  • 874,273
  • 37
  • 540
  • 662
  • can you do it using [this](https://stackoverflow.com/questions/74018046/how-can-i-rollapply-in-r-with-two-columns-of-a-data-frame/74018648#74018648) ? – Homer Jay Simpson Oct 18 '22 at 17:07
  • @HomerJaySimpson that can be done, but from your input/expected output, it seems to be lagging or leading the column values instead of windowing – akrun Oct 18 '22 at 17:09
  • I would appreciate it if you do it with slice_dfr.I see the lagging issue.You ae right.But if you can do it I would it will be great. – Homer Jay Simpson Oct 18 '22 at 17:10
  • @HomerJaySimpson the issue is also that you are doing this in reverse i.e. for 'a', it is lag' and 'b' it is lead. In that other question, I used sequence to index and that is common for both the columns. – akrun Oct 18 '22 at 17:13
  • @HomerJaySimpson in addition, your example is not doing windowing. So, why you want slide i.e. suppose you do `seq_len(nrow(df1)) %>% slide(~ .x, .after = 3, .complete = TRUE)`, you will get the index in a list i.e. first list element gets 1 to 4 rows, and second, 2:5 – akrun Oct 18 '22 at 17:15
  • I see you are right – Homer Jay Simpson Oct 18 '22 at 17:17
  • @HomerJaySimpson Do you want `Filter(length, seq_len(nrow(df1)) %>% slide(~ lead(df1$b[.x], default = last(df1$b)) + lag(df1$a[.x], default = first(df1$a)), .after = 3, .complete = TRUE))` – akrun Oct 18 '22 at 17:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248884/discussion-between-homer-jay-simpson-and-akrun). – Homer Jay Simpson Oct 18 '22 at 17:18