trt<-c(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3 ,3)
plant<-c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5 ,5, 5, 5)
change<-c(-9, -6, -4 , 7 , 4, 11, 18, 5, 29, 10, 29, 36, 10, 9, 4, -1 ,14 ,16 , 9 , 0, -2, 6,14, 18, -6, 13, 11, -1, 7, 15)
df<-data.frame(trt,plant,change)
and I want the result like this-average of the change where first 2 entries are same in trt. my data has 30 rows i want 15 in result,plz help
A base R solution using aggregate
> aggregate(change ~ ., df, mean)
trt plant change
1 1 1 -7.5
2 2 1 1.5
3 3 1 7.5
4 1 2 11.5
5 2 2 19.5
6 3 2 32.5
7 1 3 9.5
8 2 3 1.5
9 3 3 15.0
10 1 4 4.5
11 2 4 2.0
12 3 4 16.0
13 1 5 3.5
14 2 5 5.0
15 3 5 11.0