Filter next two rows

Hey Guys,

i need help.
I have a dataset with: date, action Day, packet outputs
Now i want to filter: action day = "x" and the following two days (there is no "x")

best regards

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

This is my dataset:

Date Action Day Packet Outputs
2015-01-01 x 10000
2015-01-02 - 3000
2015-01-03 - 5000
2015-01-04 - 1000
2015-01-05 - 1000
2015-01-06 x 5000
2015-01-07 - 4000
2015-01-08 - 3000
2015-01-09 - 1500
2015-01-10 - 800
2015-01-11 - 1000
2015-01-12 - 1200

Now my Question:
Packet Output at action day and the following 2 days
Result: 10000+3000+5000+5000+4000+3000 = 30.000
I need a code for this. Take the outputs from the action day (x) (2015-01-01 - 2015-01-03) (2015-01-06 - 2015-01-08) and count the outputs of the next two following days aswell.

That is not copy /paste friendly, please read the link I gave you before and reformat your example.

date action_day packet_output
01.01.15 1 10000
02.01.15 0 3000
03.01.15 0 5000
04.01.15 0 1000
05.01.15 0 1000
06.01.15 1 5000
07.01.15 0 4000
08.01.15 0 3000
09.01.15 0 1500
10.01.15 0 800
11.01.15 0 1000
12.01.15 0 1200
13.01.15 0 950
14.01.15 0 700
15.01.15 0 1000
16.01.15 0 900
17.01.15 0 800
18.01.15 0 850
19.01.15 0 1300
20.01.15 0 1100
Filtern2 <- bsp %>%
  filter(action_day == 1)

Filtern2

The missing part are the two following days but i have no idea how I can implement it.

Is this what you want to do?

bsp <- data.frame(stringsAsFactors=FALSE,
                  date = c("01.01.15", "02.01.15", "03.01.15", "04.01.15",
                           "05.01.15", "06.01.15", "07.01.15", "08.01.15",
                           "09.01.15", "10.01.15", "11.01.15", "12.01.15", "13.01.15",
                           "14.01.15", "15.01.15", "16.01.15", "17.01.15", "18.01.15",
                           "19.01.15", "20.01.15"),
                  action_day = c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                  packet_output = c(10000, 3000, 5000, 1000, 1000, 5000, 4000, 3000, 1500,
                                    800, 1000, 1200, 950, 700, 1000, 900, 800, 850, 1300,
                                    1100)
                  )
library(dplyr)

bsp %>% 
    mutate(action_day = ifelse(action_day == 1 | lag(action_day, 1) == 1 | lag(action_day, 2) == 1,
                               1, 0)) %>%
    filter(action_day == 1) %>% 
    summarise(packet_output = sum(packet_output))
#>   packet_output
#> 1         30000

Created on 2019-05-16 by the reprex package (v0.2.1)

PD: This is the proper way of sharing a reprex

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.