gpt4 book ai didi

r - 在 R 中创建并附加到数据框(错误 : arguments imply differing number of rows: 0, 1)

转载 作者:行者123 更新时间:2023-12-02 03:35:52 25 4
gpt4 key购买 nike

我在 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(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

最佳答案

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

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)

关于r - 在 R 中创建并附加到数据框(错误 : arguments imply differing number of rows: 0, 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23537586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com