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

问题描述 投票:0回答:1

我想用R来计算二元相互作用的频率表。我需要计算每个月动物之间的相互作用数量,然后总计。下面提供了一个数据样本:

#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"))

使用这些数据,我希望能够每月成对显示一个位置的动物。例如,使用这些位置的对的预期结果应该与1月份相似:

#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次。

请问最好的方法是什么?

r dplyr data.table tidyverse zoo
1个回答
1
投票

使用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
© www.soinside.com 2019 - 2024. All rights reserved.