我有一个包含4列和数千行的数据框。前两列是地理标识符,第三列是日期,最后一列是该日期的货件数量。
例如:
London UK 4/4/2018 1
London UK 4/4/2018 1
London UK 4/5/2018 3
London UK 4/5/2018 2
我想将行组合起来,以便每个城市,国家/地区和日期只有一行。
例如,上述数据将变为:
London UK 4/4/2018 2
London UK 4/5/2018 5
提前感谢您的帮助。
答案 0 :(得分:0)
以下是您的解决方案:
# 1. Data set
df <- data.frame(
country = c("UK", "UK", "UK", "UK"),
city = c("London", "London", "London", "London"),
date = c("4/4/2018", "4/4/2018", "4/5/2018", "4/5/2018"),
shipment = c(1, 1, 3, 2))
# 2. Group by 'country', 'city', and 'date' features
df %>%
group_by(country, city, date) %>%
summarise(shipment = sum(shipment))