Search code examples
rmatrixvegan

How do I reformat my matrix so that I do not lose the header name in R


I have a table with species relative abundance by year (1999:2014) and subregion (1:4). I am using FD package to compute for functional diversity using the function dbFD(x,a) where 'x' is the trait * species matrix and 'a' is the community * species abundance matrix.

relativeabun is a table with 22260 obs. of 5 variables [,1]=year, [,2]=Subregion [,3]=Species_CD [,4]=Abundance [,5] =Relative Abundance

For 'a' I need a matrix with ncol = 371 for Species_CD and nrow = 56 for my 56 communities (each year for each subregion, e.g., 1999subregion 1, 1999subregion 2, etc.)

relativeabun <- read.csv("~/Dropbox/Thesis/Functional_Diversity/Results with four Subregions/relativeabun_year_sub.csv", header = TRUE) 

for (whatarea in 1:4){ #subregions upper, middle, lower, DT
  for (whatyear in 1999:2014){

thisdata1 = relativeabun[relativeabun$Year == whatyear,]
thisdata2 = thisdata1[thisdata1$Subregion == whatarea,]
thisdata3 = thisdata2$Relative.Abundance
relabun = t(thisdata3) #transpose data so 371 columns 

functionaldiversity <- dbFD(spectrait_matrix,relabun)
    }
}

My 'a' (relabun matrix) is incorrect because it does not include the species names... should be a matrix with 371 columns with the relative abundance for each species in each of the 56 communities in [1,]

Where am I going wrong?


Solution

  • You could try the package "reshape2". What seems to work is

        library(reshape2)
        set.seed(123)
        relativeabun <- data.frame(year=rep(2010:2015,10), Subregion=rep(c("Asia", "Africa"), 30), Species_CD=rep(c("A", "B", "C", "D", "E")), Abundance=runif(60), RelAbundance=runif(60))
    

    This results in something like

        year Subregion Species_CD Abundance RelAbundance
        2010      Asia          A 0.2655087    0.9128759
        2011    Africa          B 0.3721239    0.2936034
        2012      Asia          C 0.5728534    0.4590657
        2013    Africa          D 0.9082078    0.3323947
        2014      Asia          E 0.2016819    0.6508705
        2015    Africa          A 0.8983897    0.2580168
    

    Next you need to melt this data frame and cast it back with the species in the columns

        tmp <- melt(relativeabun, id.vars=c("year", "Subregion", "Species_CD"), value.name="Value")
        relabun <- dcast(data=tmp, ...~Species_CD, fun.aggregate=sum, value.var="Value")
        relabun <- relabun[relabun$variable=="RelAbundance", -c(3)]
    

    The last line is there to get rid of the Abundance data and of the melt variable.