【问题标题】:Gathering column names from data frame into a list in R将数据框中的列名收集到 R 中的列表中
【发布时间】:2013-12-03 14:43:55
【问题描述】:

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

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

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

【问题讨论】:

    标签: r igraph vertex edge-list


    【解决方案1】:

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

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

    您可以使用

    进行检查
    get.edge.attribute(g, 'location')
    

    例如,我可以使用属性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)
    

    【讨论】:

    • 对不起,我会编辑帖子。我确实知道为边缘设置属性,但我不知道如何确保从数据框中获得的边缘列表保留位置数据(这更有意义)
    • 那么E(g)$location=colnames(df) 呢?这会将位置信息添加到每个边缘。
    • 我尝试按照您的建议进行操作,但出现了警告,但我不确定这是什么意思:Warning message: In eattrs[[name]][index] &lt;- value : number of items to replace is not a multiple of replacement length
    猜你喜欢
    • 2018-11-18
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多