简体   繁体   English

如何从参考数据框中给定条件填充空白矩阵?

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

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.我想要做的是根据个人被抓到一起的次数分配“1,2,3...或0”。

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 NA002 A一种
NA03,41 NA03,41 A一种
NA03,42 NA03,42 B
NA03,41 NA03,41 C C
NA03,42 NA03,42 C C
NA03,43 NA03,43 C C
NA002 NA002 D D
NA03,41 NA03,41 D D
NA03,44 NA03,44 D D
NA03,45 NA03,45 D 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.我无法手动执行此操作,因为完整矩阵是 389x389 单元格。

NA002 NA002 NA03,41 NA03,41 NA03,42 NA03,42 NA03,43 NA03,43 NA03,44 NA03,44 NA03,45 NA03,45
NA002 NA002 - ——
NA03,41 NA03,41 2 2 - ——
NA03,42 NA03,42 0 0 1 1 - ——
NA03,43 NA03,43 0 0 1 1 1 1 - ——
NA03,44 NA03,44 1 1 1 1 0 0 0 0 - ——
NA03,45 NA03,45 1 1 1 1 0 0 0 0 1 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...个人 NA002 和 NA03,41 被抓到一起两次,所以他们会被分配到 2。个人 NA002 和 NA03,42 从未被抓到一起,所以他们得到了 0。个人 NA002 和 NA03,44 被抓到了一次,所以他们得到了1,等等...

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.我已经使用 R 几年了,但这种类型的工作对我来说是全新的,因为我以前从未使用过矩阵。

I was thinking of using the basic我正在考虑使用基本的

net[,]= but with an ifelse statement, but am unsure how to do this. net[,]=但带有 ifelse 语句,但我不确定如何执行此操作。

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.谢谢 cgvoller,你的代码很棒。 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.如果您不能在原始 df 的一行子集中写出您的 df,那么它只有您需要的两列。

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)```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM