I am an inexperienced R user and have been struggling with the By() function and would appreciate your help. The task is simple, I have a longitudinal dataset (How do I declare one?) and need to calculate different metrics by ID. One of the the metrics is a simple percent change (that requires a lag, example below):
ID Date Temp %Change
AAA 1/1/2003 0.749881714 NA
AAA 1/2/2003 0.666661576 -0.110977687
AAA 1/3/2003 0.773079935 0.159628759
AAA 1/4/2003 0.62902364 -0.186340751
AAA 1/5/2003 0.733312374 0.165794619
BBB 1/1/2003 0.707339766 NA
BBB 1/2/2003 0.764986529 0.081497982
BBB 1/3/2003 0.662201467 -0.134361925
BBB 1/4/2003 0.774451765 0.169510798
BBB 1/5/2003 0.50829093 -0.343676453
CCC 1/1/2003 0.836836215 NA
CCC 1/2/2003 0.837136823 0.00035922
CCC 1/3/2003 0.809016624 -0.033590924
CCC 1/4/2003 0.690277509 -0.146769685
CCC 1/5/2003 0.796357069 0.153676686
Intuitively I understand the use of By(), but haven't been able to produce the correct result (%Change) using a data.frame that contains $ID, $Date, and $Temp. Any suggestions in how to achieve the desired %Change would be greatly appreciated.