0

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
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
I Del Toro
  • 913
  • 4
  • 15
  • 36

1 Answers1

3

I guess you want this:

res <- aggregate(cbind(sp1,sp2)~site, df, sum)
library(reshape2)
melt(res)
#   site variable value
# 1    A      sp1     6
# 2    B      sp1     4
# 3    C      sp1     3
# 4    A      sp2     0
# 5    B      sp2     1
# 6    C      sp2     1
Roland
  • 127,288
  • 10
  • 191
  • 288