Home > Enterprise >  How to dissolve the dataset on multiple conditions - R
How to dissolve the dataset on multiple conditions - R

Time:04-30

Consider dataset the following dataset:

ID Start time End time Traffic Lane
1 01-01-2015 01-02-2015 500 1
1 01-01-2015 01-02-2015 400 2
1 01-02-2015 01-03-2015 250 1
1 01-02-2015 01-03-2015 250 2
2 01-01-2015 01-02-2015 80 1
2 01-02-2015 01-03-2015 70 1

I want to aggregate the traffic values based on multiple conditions. The traffic values should be aggregated based on a similar ID and Start time so that the output dataset becomes:

ID Start time End time Traffic
1 01-01-2015 01-02-2015 900
1 01-02-2015 01-03-2015 500
2 01-01-2015 01-02-2015 80
2 01-02-2015 01-03-2015 70

What is a convenient way of achieving this?

CodePudding user response:

You should achieve this by grouping group_by() and then summarize accordingly, e.g.

library(dplyr)    
df %>% 
      group_by(ID, start_time, End_time) %>% 
      summarise(Traffic = sum(Traffic))
  • Related