How to collapse the rows in r

Viewed 30

The dataset is as follows:

DATE ID Value Temp
01-01-2000 1 1 21
01-01-2000 1 4 21
02-01-2000 2 9 34
02-01-2000 3 5 37
02-01-2000 3 4 37

I expect this outcome:

DATE ID Value Temp
01-01-2000 1 5 21
02-01-2000 2 9 34
02-01-2000 3 9 37

I try to use aggregate but it cannot keep the variable - Temp

Please give me some suggestions, thank you

aggregate(data$Value, by=list(Date=data$ID), FUN=sum)

The above code only returned:

DATE Value
01-01-2000 5
02-01-2000 9
02-01-2000 9
1 Answers

Your code isn't reproducible but try this:

aggregate(Value~ID, DatasetName, sum) 
Related