[R] Differencing data by groups

Thomas Miller miller at cbl.umces.edu
Fri Oct 12 21:36:05 CEST 2007


Colleagues,

I am analyzing data collected during oceanographic cruises. We have
conducted many cruises over the last decade.  On each cruise we visit ~50
stations. At each station (termed EventNum)we lower an instrument that
measures depth, temperature, salinity and oxygen every few seconds as it is
lowered through the water.  Data from all EventNums on all cruises are
stacked, generating a data file that might look like 

  CruiseID Year Season EventNum Region Depth Salinity    Temp  Oxygen
1    CF0101 2001 Spring      849  Lower 0.056  21.1753 15.5626 8.12344
2    CF0101 2001 Spring      849  Lower 0.103  21.3264 15.5511 8.11873
3    CF0101 2001 Spring      849  Lower 0.131  21.4780 15.5492 8.10568
4    CF0101 2001 Spring      849  Lower 0.169  21.5296 15.5492 8.10633
....

15000   CF0103 2001   Fall      929    Mid 14.449   17.692 22.901  7.743
15001   CF0103 2001   Fall      929    Mid 14.478   17.691 22.900  7.743
15002   CF0103 2001   Fall      929    Mid 14.527   17.692 22.899  7.743
15003   CF0103 2001   Fall      929    Mid 14.595   17.692 22.900  7.743
15004   CF0103 2001   Fall      929    Mid 14.654   17.694 22.902  7.742
15005   CF0103 2001   Fall      929    Mid 14.722   17.695 22.903  7.742
15006   CF0103 2001   Fall      929    Mid 14.790   17.695 22.903  7.742
15007   CF0103 2001   Fall      929    Mid 14.839   17.694 22.903  7.742
15008   CF0103 2001   Fall      929    Mid 14.887   17.695 22.902  7.742
15009   CF0103 2001   Fall      929    Mid 14.946   17.694 22.902  7.742
15010   CF0103 2001   Fall      929    Mid 14.985   17.693 22.902  7.743
......

In total there are ~350k rows of data

I can calculate simple EventNum averages using

meanobs<-aggregate(ctd[,7:9],by=list(EventNum=EventNum),mean)

But, I want to calculate a depth-weighted mean for salinity, temp and oxygen
for each EventNum.  The depth-weighted mean is calculated as 

Xbar = Sum{depth_interval*temp}/Sum(depth_intervals)

Where the depth interval is the differenced depths  (i.e., 0.103-0.056 for
the second row of data).

I cannot seem to get diff to work the way I want it to - advice appreciated

Tom

Thomas J. Miller, Ph.D.
Professor
Chesapeake Biological Laboratory
University of Maryland Center for Environmental Science
Solomons, MD 20688
Tel: (410) 326-7276
Fax: (410) 326-7318
WWW: hjort.cbl.umces.edu



More information about the R-help mailing list