I am trying to organize a matrix showing which individuals in a trapping set were caught together. Currently I have a blank matrix where the rows and columns are named after each individual in the data set
my_sociomatrix = matrix(, nrow = 389, ncol = 389)
diag(my_sociomatrix) <- 0
names_list<- unique(df$individual)
names_list
rownames(my_sociomatrix)<-names_list
colnames(my_sociomatrix)<-names_list
What I want to do is assign a "1,2,3… or 0" depending on how many times individuals were caught together.
I have a separate data frame with the information needed for determining which individuals were caught in each trapping event. Some individuals were caught multiple times so they appear more than once.
| individual | trap event |
|---|---|
| NA002 | A |
| NA03,41 | A |
| NA03,42 | B |
| NA03,41 | C |
| NA03,42 | C |
| NA03,43 | C |
| NA002 | D |
| NA03,41 | D |
| NA03,44 | D |
| NA03,45 | D |
I want to fill out the matrix using the reference of the data frame so the matrix would fill out like this. I can't do this by hand since the full matrix is 389x389 cells.
| NA002 | NA03,41 | NA03,42 | NA03,43 | NA03,44 | NA03,45 | |
|---|---|---|---|---|---|---|
| NA002 | - | |||||
| NA03,41 | 2 | - | ||||
| NA03,42 | 0 | 1 | - | |||
| NA03,43 | 0 | 1 | 1 | - | ||
| NA03,44 | 1 | 1 | 0 | 0 | - | |
| NA03,45 | 1 | 1 | 0 | 0 | 1 | - |
Individual NA002 and NA03,41 were caught together twice so they would get assigned a 2. Individual NA002 and NA03,42 were never caught together so they get a 0. And individual NA002 and NA03,44 were caught together once, so they get a 1, and so on...
How do I fill out my matrix using conditions based on my dataframe? I have use R for a few years, but this type of work is all new to me as I have never had to work with matrixes before.
I was thinking of using the basic
net[,]= but with an ifelse statement, but am unsure how to do this.