【问题标题】:Creating and appending to data frame in R (Error: arguments imply differing number of rows: 0, 1)在 R 中创建并附加到数据框(错误:参数暗示不同的行数:0、1)
【发布时间】:2014-05-08 09:17:16
【问题描述】:

我正在 R 中创建并附加到数据框:

dat <- data.frame(nodeA = character(), nodeB = character(), edge = numeric())
for (i in 1:length(countTable)-1){
  for (j in i+1:length(countTable)){
    vecA = as.numeric(as.character(countTable[i,]))
    vecB = as.numeric(as.character(countTable[j,]))
    nodeA = row.names(countTable[i,])
    nodeB = row.names(countTable[j,])
    corCoeff = cor(vecA , vecB , method = "spearman")
    dat = rbind(dat, data.frame(nodeA = nodeA, nodeB = nodeB, edge = corCoeff))
  }
}

其中countTable的head和结构如下:

> head(countTable)
                Norm One Two Three Four
ENST00000000233   12  28  11     4    8
ENST00000000412   23  44  37    23   45
ENST00000000442    9  12  27    10   22
ENST00000001008   18  98  61    21   31
ENST00000001567   16   7   3     9   12
ENST00000002125    2   4   4     5    1

> str(countTable)
'data.frame':   17972 obs. of  5 variables:
 $ Norm : int  12 23 9 18 16 2 4 1 22 14 ...
 $ One  : int  28 44 12 98 7 4 24 14 39 39 ...
 $ Two  : int  11 37 27 61 3 4 12 3 69 30 ...
 $ Three: int  4 23 10 21 9 5 4 3 271 9 ...
 $ Four : int  8 45 22 31 12 1 13 7 123 60 ...

如果我单独查看嵌套 for 循环中的代码,它会像我希望的那样工作。但是,当我运行整个代码时,出现错误:

Error in data.frame(nodeA = nodeA, nodeB = nodeB, edge = corCoeff) : 
  arguments imply differing number of rows: 0, 1
In addition: Warning message:
NAs introduced by coercion 

【问题讨论】:

    标签: r error-handling dataframe


    【解决方案1】:

    : 运算符的优先级高于+-。您的代码应更正为:

    for (i in 1:(length(countTable)-1)){
       for (j in (i+1):length(countTable)){
          ...
       }
    }
    

    注意两者之间的区别:

    n <- 3
    for (i in 1:n-1)
      for (j in i+1:n)
        cat(sprintf("(%g,%g)\n", i, j))
    ## (0,1)
    ## (0,2)
    ## (0,3)
    ## (1,2)
    ## (1,3)
    ## (1,4)
    ## (2,3)
    ## (2,4)
    ## (2,5)
    

    和:

    for (i in 1:(n-1))
      for (j in (i+1):n)
        cat(sprintf("(%g,%g)\n", i, j))
    ## (1,2)
    ## (1,3)
    ## (2,3)
    

    【讨论】:

    • 这对我来说很有趣,而且是新的想法。谢谢!但是,在这种情况下,更改我的代码以反映这一点仍然会输出相同的错误...
    【解决方案2】:

    你可能想要这样的东西。将countTable 转换为matrix 并下拉到一个循环,使用ii-1 作为循环索引。并且不需要事先创建一个空的数据框。

    > countTable <- as.matrix(countTable)
    > rn <- rownames(countTable)
    > dat <- do.call(rbind, lapply(2:nrow(countTable), function(i){
        corCoeff <- cor(countTable[i-1,] , countTable[i,], 
                        method = "spearman", use = "complete.obs")
        data.frame(nodeA = rn[i-1], nodeB = rn[i], edge = corCoeff)
        }))
    > dat
    #             nodeA           nodeB       edge
    # 1 ENST00000000233 ENST00000000412  0.1538968
    # 2 ENST00000000412 ENST00000000442  0.6668859
    # 3 ENST00000000442 ENST00000001008  0.7000000
    # 4 ENST00000001008 ENST00000001567 -0.8000000
    # 5 ENST00000001567 ENST00000002125 -0.5642881
    

    【讨论】:

    • 谢谢,我尝试了这段代码,但之后我没有看到任何保存在 dat 中。 (它只是 [1] nodeA nodeB edge (或 0-length row.names))
    • 结果是dat。我会编辑。无需事先创建空数据框
    • 谢谢,理查德。但是当我这样做时,我得到“错误:找不到对象'dat'”。我还收到警告“在 cor(countTable[i - 1, ], countTable[i, ], method = "spearman") 中:标准差为零"
    • 好的。我添加了use = "complete.obs" 来说明countTable 中的NA 值。希望现在一切都好。
    • 我想知道这一点。比较的是什么?列?
    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多