【发布时间】:2016-09-20 12:31:52
【问题描述】:
我目前在使用 R 比较特定矩阵中的每一列时遇到问题。我试图一次比较整个列中的每一列,并通过 table 命令生成真假输出,然后将可以找到的真数转换为数值并在它们各自的位置输入这些值关联矩阵。
For example, I have data in this type of format:
//Example state matrix - I am attempting to compare c1 with c2, then c1 with c3, then c1 with c4 and so on and so forth
c1 c2 c3 c4
r1 2 6 3 2
r2 1 1 6 5
r3 3 1 3 6
And I am trying to instead put it into this format
//Example incidence matrix - Which is how many times c1 equaled c2 in the above matrix
c1 c2 c3 c4
c1 3 1 1 1
c2 1 3 0 0
c3 1 0 3 0
c4 1 0 0 3
这里是我到目前为止想出的代码,但是,我不断收到这个特殊错误 --
警告信息: In IncidenceMat[rat][r] = IncidenceMat[rat][r] + as.numeric(instances) :要替换的项目数不是替换长度的倍数rawData = read.table("5-14-2014streamW636PPstate.txt")
colnames = names(rawData) #the column names in R
df <- data.frame(rawData)
rats = ncol(rawData)
instances = nrow(rawData)
IncidenceMat = matrix(rep(0, rats), nrow = rats, ncol = rats)
for(rat in rats)
for(r in rats)
if(rat == r){rawData[instance][rat] == rawData[instance][r] something like this would work in C++ if I attempted,
IncidenceMat[rat][r] = IncidenceMat[rat][r] + as.numeric(instances)
} else{
count = df[colnames[rat]] == df[colnames[r]]
c = table(count)
TotTrue = as.numeric(c[2][1])
IncidenceMat[rat][r] = IncidenceMat[rat][r] + TotTrue #count would go here #this should work like a charm as well
}
任何帮助将不胜感激;我也查看了其中一些资源,但是我仍然感到困惑
I tried thisand this 以及我最近关闭的一些其他资源。
【问题讨论】: