I've been struggling on R for the last two days with a simple basic operation that would take 1 min to perform in Excel. Let me explain: I have a dataframe with 65 columns (variables, the species found) and 75 rows (obs, the quadrats). The first 25 rows are quadrats of site A, then the next 25 from site B then the last 25 from site C. Each column is a species and if the species X is present in Site A1, we have a 1 and if it's not there is a zero. I would like to study the diversity index of each site and not of each quadrat which is what specnumber() and diversity() will give me. Here is an exemple of the dataframe with 5 quadrats from site A and 3 species:
Site | Quadrat | Sp X. | Sp Y. | Sp Z. |
---|---|---|---|---|
A | Quadrat A1 | 1 | 1 | 0 |
A | Quadrat A2 | 0 | 1 | 0 |
A | Quadrat A3 | 1 | 1 | 0 |
A | Quadrat A4 | 0 | 0 | 1 |
A | Quadrat A5 | 1 | 1 | 1 |
And here is what I would like to get:
Site | Sp X. | Sp Y. | Sp Z. |
---|---|---|---|
Site A | 3 | 4 | 2 |
I have tried sumRows and sum of course, as well as rbind and dozens of other things found online but nothing seems to work, I am really new to R:
new <- rbind(df, L = sum(df[1:25,]) )
new <- rowSums(df[1:25,]
Thank you so much