0

I have the following dataframe:

ID  Value   Time_hr
1   0.1     0
1   0.15    1
1   0.2     2
2   0.08    0
2   0.12    1
3   0.09    0
3   0.12    1
3   0.13    2
3   0.15    3

I will like to find the mean values of all baseline values (Time_hr=0) for all groups and same for other time frames (1, 2, 3 etc) as follows:

Time_hr Mean_val
0   0.09
1   0.13
2   0.14
3   0.15

Here's my solution that didn't work:

aggregate(df, by=list(df$Value,df$Time_r),FUN=mean)

Has anyone got a solution that will work for this problem? Thanks

Rob John
  • 277
  • 1
  • 3
  • 12
  • The `by` argument defines the group - not the group and the column to average. You need `aggregate(df$Value, by = list(df$Time_hr), FUN = mean)` for that syntax. See other options at the duplicate. – Gregor Thomas Oct 02 '20 at 21:01
  • Maybe you want `aggregate(data = df, Value ~ Time_hr, FUN = mean)`? – Ben Oct 02 '20 at 21:01
  • You could use `df %>% group_by(Time_hr) %>% summarise(Value = mean(Value))` – akrun Oct 02 '20 at 21:49
  • Thanks so much everyone. Your solutions worked. Problem now solved. – Rob John Oct 03 '20 at 22:01

0 Answers0