【问题标题】:survey data into network调查数据进入网络
【发布时间】:2021-09-22 20:44:34
【问题描述】:

我有一个类似这样的数据框:

case_id pol_demo pol_demo_online pol_post_online pol_petition pol_cntct_polit pol_party pol_other pol_demo_illegal
1311 1 1 1 1 1 1 1 1
97 0 0 0 0 0 0 0 0
5480 0 0 0 0 0 0 0 0
2531 1 1 1 1 1 1 1 1
2291 0 0 0 1 0 0 0 0
2064 1 1 1 1 1 1 1 1

case_id 对应于调查对象的唯一 id,它的唯一值与数据框中的行数一样多。列是行为形式,相应的受访者做了 (1) 或没有做 (0)。列数比图片上的多 (17),但您明白了。

我想将其转换为邻接矩阵。也就是说,我想要一个有 17 行和 17 列的矩阵(每个行为一个),并且单元格应该是组合不同形式的行为的次数(每对的总和)。因此,使用示例中的列,矩阵将如下所示:

pol_demo pol_demo_online pol_post_online pol_petition pol_cntct_polit pol_party pol_other pol_demo_illegal
pol_demo 3 3 3 3 3 3 3 2
pol_demo_online 3 3 3 3 3 3 3 2
pol_post_online 3 3 3 3 3 3 3 2
pol_petition 3 3 3 4 3 3 3 2
pol_cntct_polit 3 3 3 3 3 3 3 2
pol_party 3 3 3 3 3 3 3 2
pol_other 3 3 3 3 3 3 3 2
pol_demo_illegal 2 2 2 2 2 2 2 2

意思是pol_demo与pol_demo_online结合3次,而与pol_demo_illegal等只有2次。

我有一种预感,我需要与outer 合作,但我想不通。我会很高兴有一个整洁的解决方案,但真的,非常感谢任何帮助!

这是数据的sn-p:

dat <- structure(list(pol_demo = c(1, 0, 0, 1, 0, 1), pol_demo_online = c(1, 
0, 0, 1, 0, 1), pol_post_online = c(1, 0, 0, 1, 0, 1), pol_petition = c(1, 
0, 0, 1, 1, 1), pol_cntct_polit = c(1, 0, 0, 1, 0, 1), pol_party = c(1, 
0, 0, 1, 0, 1), pol_other = c(1, 0, 0, 1, 0, 1), pol_demo_illegal = c(0, 
0, 0, 1, 0, 1), help_shopping = c(1, 1, 0, 1, 1, 1), help_childcare = c(0, 
1, 0, 1, 0, 1), help_general = c(1, 1, 0, 1, 1, 1), help_emo = c(1, 
1, 0, 1, 1, 1), help_symb = c(1, 1, 0, 1, 0, 1), help_fin = c(1, 
0, 0, 1, 1, 1), help_donation = c(1, 0, 0, 1, 1, 1), help_volunteer = c(1, 
0, 0, 1, 0, 1), help_other = c(1, 1, 0, 1, 0, 1), case_id = structure(c(1311, 
97, 548, 2531, 2291, 2064), label = "numerical, unique ID per respondent", format.stata = "%9.0g")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

    标签: r tidyverse adjacency-matrix


    【解决方案1】:

    我在 unrelated question here. 中找到了一个解决方案,我使用 pivote_longer() 重新排列了 dat,以使其成为适用于该解决方案的格式。

    dat <- dat %>% 
      pivot_longer(cols = -case_id,
                   names_to = "name",
                   values_to = "val") %>%
      filter(val != 0) %>%
      select(id = case_id, name)
    

    然后使用为您的数据修改的链接答案中的部分。

    # create a 2-mode sociomatrix
    mat <-  t(table(dat))
    # create adjacency matrix as product of the 2-mode sociomatrix
    adj.mat <- mat %*% t(mat)
    

    编辑:最初复制并粘贴了错误的 pivot_longer() 这个作品。

    【讨论】:

      【解决方案2】:

      另一种方法是对所有列使用成对操作,然后我们只需要将输出转换为矩阵。

      有几个包可以实现配对操作:corrr::colpair_mappwiser::pairwisedplyover::across2x

      以下是使用dplyover::across2x 的一种方法(免责声明:我是维护者)。

      library(dplyr)
      library(dplyover) # https://github.com/TimTeaFan/dplyover
      
      colnms <- dat %>% select(-case_id) %>% colnames
      
      out <- dat %>% 
              summarise(across2x(-case_id,
                                 -case_id,
                                 ~ sum(.x == 1 & .y == 1)))
      
      matrix(out,
             ncol = 17,
             dimnames = list(colnms, colnms))
      
      #>                  pol_demo pol_demo_online pol_post_online pol_petition
      #> pol_demo         3        3               3               3           
      #> pol_demo_online  3        3               3               3           
      #> pol_post_online  3        3               3               3           
      #> pol_petition     3        3               3               4           
      #> pol_cntct_polit  3        3               3               3           
      #> pol_party        3        3               3               3           
      #> pol_other        3        3               3               3           
      #> pol_demo_illegal 2        2               2               2           
      #> help_shopping    3        3               3               4           
      #> help_childcare   2        2               2               2           
      #> help_general     3        3               3               4           
      #> help_emo         3        3               3               4           
      #> help_symb        3        3               3               3           
      #> help_fin         3        3               3               4           
      #> help_donation    3        3               3               4           
      #> help_volunteer   3        3               3               3           
      #> help_other       3        3               3               3           
      #>                  pol_cntct_polit pol_party pol_other pol_demo_illegal
      #> pol_demo         3               3         3         2               
      #> pol_demo_online  3               3         3         2               
      #> pol_post_online  3               3         3         2               
      #> pol_petition     3               3         3         2               
      #> pol_cntct_polit  3               3         3         2               
      #> pol_party        3               3         3         2               
      #> pol_other        3               3         3         2               
      #> pol_demo_illegal 2               2         2         2               
      #> help_shopping    3               3         3         2               
      #> help_childcare   2               2         2         2               
      #> help_general     3               3         3         2               
      #> help_emo         3               3         3         2               
      #> help_symb        3               3         3         2               
      #> help_fin         3               3         3         2               
      #> help_donation    3               3         3         2               
      #> help_volunteer   3               3         3         2               
      #> help_other       3               3         3         2               
      #>                  help_shopping help_childcare help_general help_emo help_symb
      #> pol_demo         3             2              3            3        3        
      #> pol_demo_online  3             2              3            3        3        
      #> pol_post_online  3             2              3            3        3        
      #> pol_petition     4             2              4            4        3        
      #> pol_cntct_polit  3             2              3            3        3        
      #> pol_party        3             2              3            3        3        
      #> pol_other        3             2              3            3        3        
      #> pol_demo_illegal 2             2              2            2        2        
      #> help_shopping    5             3              5            5        4        
      #> help_childcare   3             3              3            3        3        
      #> help_general     5             3              5            5        4        
      #> help_emo         5             3              5            5        4        
      #> help_symb        4             3              4            4        4        
      #> help_fin         4             2              4            4        3        
      #> help_donation    4             2              4            4        3        
      #> help_volunteer   3             2              3            3        3        
      #> help_other       4             3              4            4        4        
      #>                  help_fin help_donation help_volunteer help_other
      #> pol_demo         3        3             3              3         
      #> pol_demo_online  3        3             3              3         
      #> pol_post_online  3        3             3              3         
      #> pol_petition     4        4             3              3         
      #> pol_cntct_polit  3        3             3              3         
      #> pol_party        3        3             3              3         
      #> pol_other        3        3             3              3         
      #> pol_demo_illegal 2        2             2              2         
      #> help_shopping    4        4             3              4         
      #> help_childcare   2        2             2              3         
      #> help_general     4        4             3              4         
      #> help_emo         4        4             3              4         
      #> help_symb        3        3             3              4         
      #> help_fin         4        4             3              3         
      #> help_donation    4        4             3              3         
      #> help_volunteer   3        3             3              3         
      #> help_other       3        3             3              4
      

      reprex package (v2.0.1) 于 2021 年 9 月 23 日创建

      【讨论】:

        猜你喜欢
        • 2020-09-18
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多