# Load Libraries library(vegan) # Load data Brier_Ck<-read.csv("Brier_Ck.csv", header= T, row.names = 1) #Dimensions of the matrix (dim function) dim(Brier_Ck) #List variables in the data (names function) names(Brier_Ck) #Get the 5 most abundant species abundance<-apply(Brier_Ck,MARGIN=2,sum) #sorting and print the top 5 species sort(abundance, decreasing = T)[1:5] # Make a presence/absence matrix Brier_Ck_pa<-decostand(Brier_Ck, method="pa") # This works also Brier_Ck_pa2<-Brier_Ck[] Brier_Ck_pa2[Brier_Ck>0]<-1 # Get number of species by site site_s<-apply(Brier_Ck_pa,1,sum) site_s # eliminate sites with 2 or fewer species #save result at Brier_Ck_2 Brier_Ck_2<-Brier_Ck[site_s>2,] #eliminate species with a total abundance less than 10 #use apply to get species abundance species_abund<-apply(Brier_Ck_2,2,sum) #eliminate columns (species) with low abundance #save resul as Brier_Ck_3 Brier_Ck_3<-Brier_Ck_2[,species_abund>10] # get dimensions of final matrix dim(Brier_Ck_3) # log transform Brier_Ck_3_log<-log(Brier_Ck_3+1) #write to file write.csv(Brier_Ck_3_log,"final_matrix.csv")