Say I have a data frame, data
That contains multiple sites, indicated by integer site codes. Within those sites are samples from multiple horizons, A
,B
and C
, which have observations of some type, indicated in the column value
:
site<- c(12,12,12,12,45,45,45,45)
horizon<-c('A','A','B','C','A','A','B','C')
value<- c(19,14,3,2,18,19,4,5)
comment<- c('pizza','pizza','pizza','pizza','taco','taco','taco','taco')
data<- data.frame(site,horizon,value,comment)
Which looks like this:
site horizon value comment
1 12 A 19 pizza
2 12 A 14 pizza
3 12 B 3 pizza
4 12 C 2 pizza
5 45 A 18 taco
6 45 A 19 taco
7 45 B 4 taco
8 45 C 5 taco
In this case both sites have multiple A
observations. I would like to average the values of of duplicate horizons within a site. I would like to retain the comment line within the data frame as well. All observations within a site have the same entry within the comment vector. I would like the output to look like this:
site horizon value comment
1 12 A 16.5 pizza
3 12 B 3 pizza
4 12 C 2 pizza
5 45 A 18.5 taco
7 45 B 4 taco
8 45 C 5 taco