R-重新整形数据,用于绘制网络



我有一个数据集,我想重新形状以将其绘制为网络(遵循此处完成的工作)。初始数据框架看起来像这样:

authors <- c('Author A', 'Author B', 'Author C', 
             'Author A', 'Author D', 'Author C')
affiliation <- c('University 1', 'University 2', 'University 1', 
                 'University 1', 'Institute 3', 'University 1')
manuscript <- c('Manuscript A', 'Manuscript A', 'Manuscript A', 
                'Manuscript B', 'Manuscript B', 'Manuscript B')
df <- data.frame(authors, affiliation, manuscript)

我想重新塑造它,以便对于每个手稿,我都可以通过主要作者的隶属关系来获得所有作者的组合(我希望我问这个问题的方式是有道理的)。这将导致以下数据框架:

df_network <- data.frame('primary_author'= c('Author A', 'Author A', 
                                             'Author B', 'Author B', 
                                             'Author C', 'Author C', 
                                             'Author A','Author A', 
                                             'Author D', 'Author D', 
                                             'Author C', 'Author C'),
                         'connection'= c('Author B', 'Author C', 
                                         'Author A', 'Author C', 
                                         'Author A', 'Author B', 
                                         'Author D', 'Author C', 
                                         'Author A', 'Author C', 
                                         'Author A', 'Author D'),
                         'primary_affiliation' = c('University 1', 'University 1',
                                                   'University 2', 'University 2',
                                                   'University 1', 'University 1',
                                                   'University 1', 'University 1',
                                                   'Institute 3', 'Institute 3',
                                                   'University 1', 'University 1'),
                         'manuscript' = c('Manuscript A', 'Manuscript A',
                                          'Manuscript A', 'Manuscript A',
                                          'Manuscript A', 'Manuscript A',
                                          'Manuscript B', 'Manuscript B',
                                          'Manuscript B', 'Manuscript B',
                                          'Manuscript B', 'Manuscript B'))

当然,我可以手工重新分配数据,但这非常乏味,尤其是在列表变得很长的时候。我以前(手动)做过此操作,如果我可以以df_network的形状获取数据,那么结果非常好。任何人都可以提供的任何提示或技巧将不胜感激。

尝试以下:

library(dplyr)
df %>% 
  left_join(df, by = "manuscript") %>% 
  filter(!authors.x == authors.y) %>% 
  select(primary_author = authors.x, 
         connection = authors.y, 
         primary_affiliation = affiliation.x, 
         manuscript)

输出:

   primary_author connection primary_affiliation   manuscript
1        Author A   Author B        University 1 Manuscript A
2        Author A   Author C        University 1 Manuscript A
3        Author B   Author A        University 2 Manuscript A
4        Author B   Author C        University 2 Manuscript A
5        Author C   Author A        University 1 Manuscript A
6        Author C   Author B        University 1 Manuscript A
7        Author A   Author D        University 1 Manuscript B
8        Author A   Author C        University 1 Manuscript B
9        Author D   Author A         Institute 3 Manuscript B
10       Author D   Author C         Institute 3 Manuscript B
11       Author C   Author A        University 1 Manuscript B
12       Author C   Author D        University 1 Manuscript B

您也可以将data.table用于此任务:

library('data.table')
df <- data.table(authors, affiliation, manuscript)
df <- merge(
  df, 
  df,
  by = 'manuscript', allow.cartesian = TRUE)[authors.x != authors.y, 
                                             .(primary_author = authors.x,
                                               connection = authors.y, 
                                               primary_affiliation = affiliation.x,
                                               manuscript)]

相关内容

  • 没有找到相关文章

最新更新