0

I have a data frame with 4 columns and several thousands rows. The first two columns are geographical identifiers, the third one is a date, and the last one is the number of shipments in that date.

For example:

London UK 4/4/2018 1
London UK 4/4/2018 1
London UK 4/5/2018 3
London UK 4/5/2018 2

I would like to combine the rows so as to have only one row per city, country, and date.

For example, the above data would become:

London UK 4/4/2018 2
London UK 4/5/2018 5

Thank you for all help in advance.

1 Answers1

0

Here is your solution:

# 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))
Andrii
  • 2,843
  • 27
  • 33