【问题标题】:Simple conversion to edgelist with R?使用 R 简单转换为边缘列表?
【发布时间】:2012-03-24 09:17:24
【问题描述】:

我需要在 R 中进行简单的数据转换,以便与 igraph 一起使用。我的数据框是这种格式,按GROUP分组:

    A   GROUP
1   1       a
2   2       a
3   3       a
4   4       a
5   1       b
6   3       b
7   5       b

1。如何扩展组以获得这种格式的无向边缘列表el

    A   B
1   1   2
2   1   3
3   1   4
4   2   3
5   2   4
6   3   4
7   1   3
8   1   5
9   3   5

注意:没有自引用 1-1, 2-2, 3-3, ...

2。如何计算 A-B 出现次数并从 el 创建加权边缘列表?

    A   B   weight
1   1   2        1
2   1   3        2
3   1   4        1
4   2   3        1
5   2   4        1
6   3   4        1
7   1   5        1
8   3   5        1

【问题讨论】:

    标签: r igraph data-conversion edges


    【解决方案1】:

    这里有一个解决方案,我在代码中注释了:

    # your data
    df <- data.frame(A = c(1, 2, 3, 4, 1, 3, 5),
                 GROUP = c("a", "a", "a", "a", "b", "b", "b"))
    
    # define a function returning the edges for a single group
    group.edges <- function(x) {
      edges.matrix <- t(combn(x, 2))
      colnames(edges.matrix) <- c("A", "B")
      edges.df <- as.data.frame(edges.matrix)
      return(edges.df)
    }
    
    # apply the function above to each group and bind altogether
    all.edges <- do.call(rbind, lapply(unstack(df), group.edges))
    
    # add weights
    all.edges$weight <- 1
    all.edges <- aggregate(weight ~ A + B, all.edges, sum)
    all.edges
    #   A B weight
    # 1 1 2      1
    # 2 1 3      2
    # 3 2 3      1
    # 4 1 4      1
    # 5 2 4      1
    # 6 3 4      1
    # 7 1 5      1
    # 8 3 5      1
    

    【讨论】:

      【解决方案2】:

      这是一种使用plyr 获取边缘列表的方法:

      foo <- data.frame(
        A = c(1,2,3,4,1,3,5),   
        GROUP = c("a","a","a","a","b","b","b"))
      
      library("plyr")
      
      E1 <- do.call(rbind,dlply(foo,.(GROUP),function(x)t(combn(x$A,2))))
      
      E1
      

      返回:

            [,1] [,2]
       [1,]    1    2
       [2,]    1    3
       [3,]    1    4
       [4,]    2    3
       [5,]    2    4
       [6,]    3    4
       [7,]    1    3
       [8,]    1    5
       [9,]    3    5
      

      然后得到权重(这里我使用 combn 将最小的数字放在第一位):

      W <- apply(E1,1,function(x)sum(E1[,1]==x[1]&E1[,2]==x[2]))
      E2 <- cbind(E1,weight=W)
      E2 <- E2[!duplicated(E2),]
      
      E2
      

      返回:

               weight
      [1,] 1 2      1
      [2,] 1 3      2
      [3,] 1 4      1
      [4,] 2 3      1
      [5,] 2 4      1
      [6,] 3 4      1
      [7,] 1 5      1
      [8,] 3 5      1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多