I need to aggregate rows of a data frame with MANY rows in this manner:
Lets say I have two rows that I want to merge (as they have the same first element):
x1=c(ID1,1,3,2,1,NA);
x2=c(ID1,2,2,3,NA,NA);
I want to merge their values so, that the result will in the case of clash (clash=both rows have a non-NA value in the element) prefer the value in a row with higher value in 2nd element (that is x2).
So in the example the result of the aggregation of these particular two rows will be:
x12=c(ID1,2,2,3,1,NA)
Can anyone please help?
I tried aggregate()
, but it offers just summary statistics like mean, max etc.