使用 R 创建变量之间交互频率的数据

Create data of frequency of interactions between variables using R

我想使用 R 计算 table 的二元交互频率。我需要计算每个月动物之间的交互次数,然后计算总数。下面提供了数据示例:

#Create sample data
B1 <-data.frame(Animal = c("A","B","C","D","E","A","B","C","D","E","A","B","C","D","E","A","B","C","D","E","A","B","C","D","E"), Location = c(1,1,2,1,3,4,2,1,1,3,3,4,3,1,1,4,2,2,2,1,1,3,4,3,2), Month = c("Jan","Jan","Jan","Jan","Jan","Feb","Feb","Feb","Feb","Feb","Mar","Mar","Mar","Mar","Mar","Apr","Apr","Apr","Apr","Apr","May","May","May","May","May"))

使用此数据,我希望能够显示每月成对出现在某个位置的动物 例如,使用这些位置的对的预期结果应该类似于一月份的结果:

#Sample extract for January
B1Jan <- data.frame(Animal1= c("A", "A","B") ,Animal2=c("B","D","D") )
B1Jan
  Animal1 Animal2
1       A       B
2       A       D
3       B       D

提取每个月后,我希望能够计算每对之间的总交互次数,例如也许A-D交互总共发生了3次

请问最好的方法是什么?

使用 data.table,您可能可以执行以下操作:

library(data.table)

#convert into data.table
setDT(B1)

#create interaction between animals in the same location & month    
ans <- B1[, if (.N > 1L) transpose(combn(unique(Animal), 2L, simplify=FALSE)), 
    by=.(Location, Month)]

#change column names to desired column names
setnames(ans, paste0("V", 1L:2L), paste0("Animal", 1L:2L))

#sort animals so that A, B and B, A are the same
ans[, paste0("Animal", 1L:2L) := .(pmin(Animal1, Animal2), pmax(Animal1, Animal2))]

#count the number of interactions as requested
ans[, .(NumInteract=.N), by=c(paste0("Animal", 1L:2L))]

输出:

   Animal1 Animal2 NumInteract
1:       A       B           1
2:       A       D           1
3:       B       D           3
4:       C       D           2
5:       A       C           1
6:       D       E           1
7:       B       C           1