0

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
doraemon
  • 439
  • 3
  • 10
  • Hello and welcome to SO! sharing your code would help (makes your problem reproducible) Also! in your original dataset there is no ```ID 3, Value 9, Temp 37``` combination which does exist in your expected outcome – Omniswitcher Sep 20 '22 at 07:52
  • I mean sum the value variable with same id, date and temp – doraemon Sep 20 '22 at 08:04
  • @user438383 I read the post and that post only related two variables, which can be solved by aggregate easily. However, I want to sum a specific column and also keep the other columns... – doraemon Sep 20 '22 at 08:08
  • `aggregate(Value ~ DATE + ID + Temp, df, sum) ` – Darren Tsai Sep 20 '22 at 08:21

1 Answers1

0

Your code isn't reproducible but try this:

aggregate(Value~ID, DatasetName, sum) 
Arduan
  • 253
  • 1
  • 11