I have a matrix of pairwise comparisons of all plots in my dataset. Matrix fill represents shared species among plots.
Plot4 Plot5 Plot6 Plot7 Plot8 Plot9 Plot10
Plot4 NA NA NA NA NA NA NA
Plot5 0 NA NA NA NA NA NA
Plot6 1 0 NA NA NA NA NA
Plot7 0 0 0 NA NA NA NA
Plot8 0 1 0 0 NA NA NA
Plot9 0 1 0 0 2 NA NA
Plot10 0 0 0 0 1 1 NA
This matrix came from the following dataframe:
data<-
region plot species
1 104 A_B
1 105 B_C
1 106 A_B
1 107 C_D
2 108 B_C
2 108 E_F
2 109 B_C
2 109 E_F
2 110 E_F
These plots are associated with certain regions. I generated the following loop that creates this pairwise comparison matrix for all 500 plots:
plots<-unique(data$plot)
plot.num<-length(plots)
output<-matrix(0, plot.num, plot.num)
for (i in 1:plot.num) {
for (j in 1:plot.num) {
plot_i<-data[data$plot==plots[i],]
plot_j<-data[data$plot==plots[j],]
output[i,j]<-length(intersect(plot_i$species, plot_j$species))
}
}
F.mat<-output
F.mat[lower.tri(F.mat, diag=T)]<-0
However, now I want to create a loop that subsets the larger matrix above by region to make a list of regional matrices.
output<-
[[1]]
Plot4 Plot5 Plot6 Plot7
Plot4 NA NA NA NA
Plot5 0 NA NA NA
Plot6 1 0 NA NA
Plot7 0 0 0 NA
[[2]] Plot8 Plot9 Plot10
Plot8 NA NA NA
Plot9 2 NA NA
Plot10 1 1 NA
NOTE: This is a quantitative matrix not presence/absence.