-4

I am trying to calculate both Simpson and Shannons diversity indices for a data set i have. My data is set out whereby my data is organised as species (observations) in rows and sites within a column. I am struggling to find any package that can be used as from what i have seen they always call for data organised with 'sites' as rows and 'species' as columns which is not how my data is organised.

Is there anyway I could calculate the diversity indices stated above with my data organised in the classic R way?

Here's a picture of my data. Each observation is a row

my data is organised in such a way, where each row is a new observation

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • Welcome to stackoverflow.com! Please read [How to ask](https://stackoverflow.com/help/how-to-ask) and [How to make reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It would be better not to post the screenshot of your data but use `dput`, see the references above. – Artem Aug 11 '18 at 19:04

1 Answers1

0

You can use dcast from reshape2 to transform your community data into the format acceptable by e.g. vegan library.

# model data
set.seed(123)
genus <- c("Arothron", "Pomacentrus","Halichoerus")
transect <- LETTERS[1:10]
df <- data.frame(transect = sample(transect, 1000, replace = TRUE), 
  genus = sample(genus, 1000, replace = TRUE))

# reshape data
library(reshape2)
df$Freq = 1
spe <- dcast(df, transect ~ genus, value.var = "Freq", fun.aggregate = sum)
rownames(spe) <- spe[, 1]
spe <- spe[, -1]

# calculate indices
library(vegan)
diversity(spe, index = "shannon")
diversity(spe, index = "simpson")
Artem
  • 3,304
  • 3
  • 18
  • 41