lundi 23 mars 2020

Conditinal aggregating by pair in R

I am trying to aggregate in R a dataframe based on condition. My dataframe looks like this:

year id crash_x crash_y crash_z injured_x injured_y injured_z
2005 1    0       1       0         0        0          3
2005 2    2       0       0         1        0          0
2005 3    0       0       3         0        2          0
2005 4    0       0       1         0        1          0
2005 5    4       0       0         3        0          0
2005 6    0       1       0         0        0          2

I would like to aggregate rows only if the columns crash_ and injured_ have the same non_null columns, so only if the columns crash_ and injured_ are matching.

The output should look like:

year crash_x crash_y crash_z injured_x injured_y injured_z
2005     0       2       0         0        0          5
2005     6       0       0         4        0          0
2005     0       0       4         0        3          0

Is this possible ? Thanks!!

Aucun commentaire:

Enregistrer un commentaire