Dear R experts, how are you?

I have a data bank in long format as below:

species month        year       plot        density

sp1         1              2001       1              39

sp2         1              2001       1              6

sp3         1              2001       1              17

sp2         1              2001       2              35

sp3         1              2001       2              15

sp4         1              2001       2              27

sp1         2              2001       1              29

sp3         2              2001       1              49

sp4         2              2001       1              1

sp1         2              2001       2              8

sp4         2              2001       2              20

sp5         2              2001       2              41

...            ...            ...            ...            ...

I want to estimate the mean density of species in each month/year. So I
want to take the mean density of the plots for each month/year (there are
more than two plots). I used the function aggregate to make that

aggregate (density~species*month*year, mean, data=df)

  species month year density

1     sp1     1      2001    39.0

2     sp2     1      2001    20.5

3     sp3     1      2001    16.0

4     sp4     1      2001    27.0

5     sp1     2      2001    18.5

6     sp3     2      2001    49.0

7     sp4     2      2001    10.5

8     sp5     2      2001    41.0


Not all species were sampled in all plots, and for those unsampled species
the estimated density should be zero for that plot. But the function
aggregate won´t make that. It will just look for densities of sampled
species and if it finds records of that species in two or more plots, then
it will take the average, but if it find only one value it will use that
unique value and the mean density will be biased.

So I think I should first transform this data to wide format, including all
combinations of species, month, year and plots, include zero where is NA´s,
retransform back to long format and continue with the analysis. But I don´t
know how to make it.

Thank you very much for any help and all the best for 2015!!

-- 
                                               Diego PJ

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to