如何在多个条件下解散数据集 - R
How to dissolve the dataset on multiple conditions - R
将数据集视为以下数据集:
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
我想根据多种条件聚合流量值。应根据类似的 ID
和 Start
时间聚合流量值,以便输出数据集变为:
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
实现此目的的简便方法是什么?
您应该通过分组 group_by()
然后相应地 summarize
来实现这一点,例如
library(dplyr)
df %>%
group_by(ID, start_time, End_time) %>%
summarise(Traffic = sum(Traffic))
将数据集视为以下数据集:
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 |
我想根据多种条件聚合流量值。应根据类似的 ID
和 Start
时间聚合流量值,以便输出数据集变为:
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 |
实现此目的的简便方法是什么?
您应该通过分组 group_by()
然后相应地 summarize
来实现这一点,例如
library(dplyr)
df %>%
group_by(ID, start_time, End_time) %>%
summarise(Traffic = sum(Traffic))