I have a large species matrix with counts that I want to express as ratio to the reference plots.  I want to use a heatmap to display change in species as a ratio to the reference

the layout is samples by rows and species by columns, it's currently a data.frame with row.name=sample name, header=species name, here's part of the table for one site (I have 36 sites).


        Otu00209        Otu00358        Otu00359        Otu00388        Otu00389        Otu00533        Otu00608        Otu00618        Otu00669        Otu00695        Otu00696        Otu00706        Otu00725        Otu00761
A7001   4       95      1       0       0       7       1       0       1       0       4       0       0       0
A7002   0       8       0       0       0       0       1       0       0       0       0       0       0       4
A7003   8       156     0       4       0       3       3       0       0       0       3       0       0       0
A7004   1       34      0       1       0       0       4       91      0       0       1       0       3       1
A7007   9       9       6       3       0       21      0       0       8       0       1       2       1       0
A7008   1       8       0       0       0       3       0       10      0       0       0       0       0       1
A7009   31      9       0       0       0       0       9       0       4       0       1       0       0       0
A7010   3       1       1       1       0       1       1       0       0       0       2       0       0       0
A7011   3       1       1       0       0       6       2       0       1       0       0       0       0       0
A7012   6       1       0       3       0       13      0       0       0       0       2       0       0       0
A7015   13      4       0       9       1       0       0       0       0       1       3       1       1       1
A7016   1       0       0       1       0       1       32      0       0       0       0       0       0       0
A7019   7       0       0       2       0       2       13      0       0       0       2       0       2       0
A7020   3       2       0       27      0       2       1       0       0       0       0       0       0       0
A7021   13      3       0       0       0       1       58      0       0       0       0       0       0       0
A7022   0       8       0       1       0       0       3       0       0       0       0       0       0       0
A7023   23      3       0       0       0       2       27      0       1       0       1       0       0       0
A7024   4       10      0       1       0       2       6       0       0       0       3       0       0       0
A7026   0       0       0       1       0       5       0       0       0       0       1       0       0       0
A7027   0       0       1       0       0       0       0       0       0       0       7       0       0       0
A7028   2       1       0       6       0       0       0       0       0       0       1       0       0       0
A7029   17      8       1       2       0       2       3       0       0       0       19      2       0       0
A7030   13      0       0       0       0       1       0       0       0       0       0       0       0       1


Environmental data is in a separate dataframe, here is one site as an example:

fung_sample     site    om      horizon
A7001   A7      2       1
A7002   A7      2       2
A7003   A7      3       1
A7004   A7      3       2
A7007   A7      1       1
A7008   A7      1       2
A7009   A7      2       1
A7010   A7      2       2
A7011   A7      3       1
A7012   A7      3       2
A7015   A7      1       1
A7016   A7      1       2
A7019   A7      2       1
A7020   A7      2       2
A7021   A7      3       1
A7022   A7      3       2
A7023   A7      1       1
A7024   A7      1       2
A7026   A7      0       2
A7027   A7      0       1
A7028   A7      0       2
A7029   A7      0       1
A7030   A7      0       2


So I need to accomplish a few of things,
    1. average the species counts for each horizon for the reference samples (om=0)  ## I was able to do this by subsetting the reference into their own matrix then using reshape to get the mean per site/horizon, but don't know if a separate matrix is really what I want
    2. add a constant to each species to prevent errors from dividing by zero
    3. divide species counts in horizon 1 by the average reference for horizon 1

I've looked at tapply which seems to be the direction that I need to go, but I'm not getting how.

Thanks

Kendra

	[[alternative HTML version deleted]]

