I have a two column dataframe of two variables that are factors:
df
PLOT INTERACTION
A interact_type_1
A interact_type_2
B interact_type_3
B interact_type_4
C interact_type_1
D interact_type_4
E interact_type_1
E interact_type_2
E interact_type_3
E interact_type_4
I need a pairwise matrix where nrows and mcolumns are the unique levels of Variable 1 (PLOTS). The matrix fill will include the counts of INTERACTION matches among each combination of the levels of PLOT. Since it is a similarity matrix there would be only 1/2 fill of the matrix, so the same PLOTS and 1/2 of the matrix will be filled with NAs. In this example, the output matrix would look like:
output
A B C D E
A NA NA NA NA NA
B 0 NA NA NA NA
C 1 0 NA NA NA
D 0 1 0 NA NA
E 2 2 1 1 NA
I have tried changing it from long to wide format then using a loop:
df<- spread(df, df$PLOT, df$INTERACTION)
similarity.matrix<-matrix(nrow=ncol(F.data),ncol=ncol(F.data))
for( in 1:ncol(F.data)){
matches<-F.data[,col]==F.data
match.counts<-colSums(matches)
match.counts[col]<-0 # Set the same column comparison to zero.
similarity.matrix[,col]<-match.counts
}
but I get an error with the first line stating Error: Invalid column specification.
I appreciate your time and help! Thank you.