'How to dissolve the dataset on multiple conditions - R
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?
Solution 1:[1]
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))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
