How do I fill a blank matrix given conditions from a refernce dataframe?

Viewed 102

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.

2 Answers

This gets the frequency for individuals sharing trap event

 library(dplyr)
 library(tidyverse)
df = data.frame(individual = c("NA002","NA03,41","NA03,42","NA03,41","NA03,42","NA03,43","NA002","NA03,41","NA03,44","NA03,45"),"trap event" = c("A","A","B","C","C","C","D","D","D","D"))

      df %>% mutate(n = 1) %>% 
      spread(individual, n, fill=0) %>% 
      select(-trap_event) %>% 
      {crossprod(as.matrix(.))} %>% 
      `diag<-`(0)

Output:

       NA002 NA03,41 NA03,42 NA03,43 NA03,44 NA03,45
NA002       0       2       0       0       1       1
NA03,41     2       0       1       1       1       1
NA03,42     0       1       0       1       0       0
NA03,43     0       1       1       0       0       0
NA03,44     1       1       0       0       0       1
NA03,45     1       1       0       0       1       0

Thank you cgvoller, your code was great. Here is the slightly edited code for anyone interested. If you can't write out your df in one line subset your original df so it only has the two columns you need.

df2 <- Rsnappers[, c("trap_event", "individual")]
head(df2)

df2 %>% mutate(n = 1) %>% 
  spread(individual, n, fill=0) %>% 
  select(-trap_event) %>% 
  {crossprod(as.matrix(.))} %>% 
  `diag<-`(0)

#add to matrix
my_sociomatrix <- df2 %>% mutate(n = 1) %>% 
  spread(individual, n, fill=0) %>% 
  select(-trap_event) %>% 
  {crossprod(as.matrix(.))} %>% 
  `diag<-`(0)```
Related