I have a dataset, df, that looks like this but has a few million instances:
Date AD Runway MTOW nr.flights
2008-01-01 A 18 376 2
2008-01-01 A 18 376 2
2008-01-01 D 36 190 1
2008-01-02 D 09 150 2
2008-01-02 A 36 280 1
2008-01-02 A 36 280 1
And I want it to look like this:
Date AD Runway MTOW nr.flights
2008-01-01 A 18 752 4
2008-01-01 D 36 190 2
2008-01-02 D 9 150 2
2008-01-02 A 36 560 1
Basically I want to group together all the Date, AD and Runway rows that are the same, so all the duplicates are removed. At the same time, I want the MTOW and nr.flights to be summed up for that particular Date, AD and Runway.
I've tried this:
vals <- expand.grid(Date = unique(df$Date),
Runway = unique(df$Runway),
AD = unique(df$AD))
So I could merge this with the original dataset, df, but that didn't work. I have also tried a few combinations of group_by but that also didn't give me the result that I wanted.
To reproduce:
df <- data.frame(Date=c("2008-01-01","2008-01-01","2008-01-01","2008-01-02","2008-01-02","2008-01-02"),
AD = c("A", "A", "D", "D", "A", "A"), Runway = c(18, 18, 36, 09, 36,36),
MTOW = c(376, 376, 190, 150, 280, 280), nr.flights = c(2,2,1,2,1,1))
Any help would be much appreciated!