调查数据入网

survey data into network

我有一个类似于此的数据框:

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是3次结合pol_demo_online,但只有2次结合pol_demo_illegal等

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

这是数据片段:

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

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

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()这个有效。

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

有几个包可以实现成对运算: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-09-23 创建