在R中将矩阵的对角线设置为零

时间:2012-09-18 06:52:44

标签: r

我在这里尝试了一些提议的解决方案。但这对我的案子不起作用。 我有一个代码,在这里:

a <- read.table("Whirr_127.csv", header=T, sep=",", row.names=1) # task assignment / people vs task
b <- read.table("Files_Whirr_127.csv", header=T, sep=",", row.names=1) #task vs files 
a
b

#calc cr , cr = ta * tf * transpose(ta)
cr <- as.matrix(a) %*% (as.matrix(b) %*% as.matrix(t(b)) %*% as.matrix(t(a)))
cr

#set value to 1, to initialize table
cr[cr>=1]<-1
cr

#identify diagonal matrix, set to zero
cr<-as.matrix(0,ncol=ncol(cr),nrow=nrow(cr))
cr<-diag(cr,x=0)

我想将对角线值设置为零。似乎最后两行中使用的代码对我的情况不起作用。

另外,我想在a中使用文件名,并将其保存为AB_Files_Whirr_127.csv 我试着用

write.csv(cr,file = paste("CR_", a,".csv")

但是,我的目录中没有任何内容。

cr的示例输出:

               Adrian Cole Alison Wong Andrei Savu Bruno Dumon Edward J. Yoon Eugene Koontz Jakob Homan Kelvin Kakugawa Kirk True Lars George Soren Macbeth Stu Hood
Adrian Cole               0           0           0           0              0             0           0               0         0           0             0        0
Alison Wong               0           0           0           0              0             0           0               0         0           0             0        0
Andrei Savu               0           0           1           0              0             0           0               0         0           1             1        0
Bruno Dumon               0           0           0           0              0             0           0               0         0           0             0        0
Edward J. Yoon            0           0           0           0              0             0           0               0         0           0             0        0
Eugene Koontz             0           0           0           0              0             0           0               0         0           0             0        0
Jakob Homan               0           0           0           0              0             0           0               0         0           0             0        0
Kelvin Kakugawa           0           0           0           0              0             0           0               0         0           0             0        0
Kirk True                 0           0           0           0              0             0           0               0         0           0             0        0
Lars George               0           0           1           0              0             0           0               0         0           1             1        0
Soren Macbeth             0           0           1           0              0             0           0               0         0           1             1        0
Stu Hood                  0           0           0           0              0             0           0               0         0           0             0        0
Tibor Kiss                0           0           0           0              0             0           0               0         0           0             0        0
Tom White                 0           0           1           0              0             0           0               0         0           1             1        0
Unassigned                0           0           0           0              0             0           0               0         0           0             0        0
                Tibor Kiss Tom White Unassigned
Adrian Cole              0         0          0
Alison Wong              0         0          0
Andrei Savu              0         1          0
Bruno Dumon              0         0          0
Edward J. Yoon           0         0          0
Eugene Koontz            0         0          0
Jakob Homan              0         0          0
Kelvin Kakugawa          0         0          0
Kirk True                0         0          0
Lars George              0         1          0
Soren Macbeth            0         1          0
Stu Hood                 0         0          0
Tibor Kiss               0         0          0
Tom White                0         1          0
Unassigned               0         0          0

1 个答案:

答案 0 :(得分:12)

a不能在输出文件的名称中使用,因为它不是字符变量,而是数据框。

infile <- "Whirr_127.csv"
a <- read.table(infile, header=T, sep=",", row.names=1)
....
diag(cr) <- 0
write.csv(cr, file = paste0("CR_", infile, ".csv")

diag行的语法往往对新R用户看起来很有趣,但它实际上只是调用赋值函数diag<-的替代语法,即diag(x) <- 0被解释为diag<-(x, 0)

更新:多个文件

如果您想对多个配对文件重复上述操作,可以执行此操作。

a.files <- grep("^Whirr", dir(), value=TRUE)
b.files <- paste0("Files_", a.files)
for(i in length(a.files)){
    a <- read.table(a.files[i], ...)
    b <- read.table(b.files[i], ...)
    ...
    write.csv(cr, paste0("CR_", a.files[i], ".csv"))
}