My dataset features several blocks, each containing several plots. In each plot, three different lifeforms were marked as present/absent (i.e. 1/0):
Block | Plot | tree | bush | grass |
---|---|---|---|---|
1 | 1 | 0 | 1 | 0 |
1 | 2 | 1 | 1 | 1 |
1 | 3 | 1 | 1 | 1 |
2 | 1 | 0 | 0 | 1 |
2 | 2 | 0 | 0 | 1 |
2 | 3 | 1 | 0 | 1 |
I'm looking for a code that will sum the total number of counts for each distict lifeform at the block level.
I would like an output that resembles this:
Block | tree | bush | grass |
---|---|---|---|
1 | 2 | 3 | 2 |
2 | 1 | 0 | 3 |
I have tried this many ways but the only thing that comes close is:
aggregate(df[,3:5], by = list(df$block), FUN = sum)
However, what this actually returns is:
Block | tree | bush | grass |
---|---|---|---|
1 | 7 | 7 | 7 |
2 | 4 | 4 | 4 |
It appears to be summing all columns together instead of keeping the lifeforms separate.
I feel as though this should be so simple, as there are many queries online about similar processes, but nothing I try has worked.