I have a site by species matrix that looks like this:
colA= c("A","A","A","B","B","B","C","C","C")
colB= c(1,2,3,3,0,1,2,1,0)
colC= c(0,0,0,1,0,0,0,0,1)
df= data.frame (site=colA, sp1=colB, sp2=colC)
This matrix has three sites A,B,C, and two species sp1, sp2.
I am trying to build code that summarizes the abundance of each species by site. The ultimate output should be a dataframe with one column having the site and a second column having the species name and the third column with the abundance of that species at that site.
I'm guessing this will have to be done in a loop for each site and then have the summaries put into a new dataframe.
The output should look something like this:
colA= c("A","B","C","A","B","C")
colB= c("sp1","sp1","sp1","sp2","sp2","sp2")
colC= c(6,4,3,0,1,1)
output= data.frame (site=colA, species=colB, abundance=colC)
output