将数据框中的列名收集到R中的列表中

问题描述 投票:0回答:1

所以我现在有一个有效的数据框,但是我想研究数据框主题的起源。这是我用来创建数据框的一些代码:

df <- as.data.frame(t(test)) #This is so the row names are products
    vertices <- row.names(df)
    place <- colnames(df)
    L <- length(vertices)
    numedges <- choose(L,2)
    edges <- data.frame(v1=rep(NA, numedges), v2=NA, numrows=NA, location=NA)
    k <- 0
    for(i in 1:(L-1)) {
    for(j in (i+1):L) {
      k <- k + 1
      edges$v1[k] <- vertices[i]
      edges$v2[k] <- vertices[j]
      edges$numrows[k] <- sum(df[vertices[i], ]=="Yes" & df[vertices[j], ]=="Yes")
      edges$location[k] ### Here is my problem!!!
    }}

而且我希望输出看起来像:

edges
          v1                    v2     numrows location #What I would like to see
1        Fish                 Squid       8    Town 1, Town 2, Town 4 
2        Fish                Fruits       0    Town 1
3        Fish                  Wood       0    Town 1
4        Fish                   Etc       2    Town 1, Town 2
5        Fish                  Corn       1    Town 1

我认为数字变成所有边的总和?如果我错了,请纠正我。因此,我想收集所有满足numrow函数的位置。

r igraph vertex edge-list
1个回答
0
投票

问题尚不清楚。您不知道如何向图形边缘添加属性?例如,要添加属性位置,只需执行以下操作:

E(g)$location=colnames(df)  ## g is your graph

您可以使用]进行检查>

get.edge.attribute(g, 'location')

例如,我可以使用属性位置来设置边的标签。

library(igraph)
g <- graph.ring(5)
V(g)$size <- 5
E(g)$location=paste(letters[1:5],LETTERS[1:5],sep=':')
E(g)$label <- get.edge.attribute(g, 'location') 
E(g)$label.cex <- 2
plot(g)

“在此处输入图像描述”

© www.soinside.com 2019 - 2024. All rights reserved.