user1676484
user1676484

Reputation: 197

Set diagonal of a matrix to zero in R

I have tried a few proposed solution in here. But it was not working for my case. I have a code, here:

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)

I want to set diagonal value as zero. It seems the code used in the last two lines are not working for my case.

Also, I would like to used the file name in a, and saved it as AB_Files_Whirr_127.csv I tried to use

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

but, nothing appear in my directory.

sample output for 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

Upvotes: 16

Views: 22233

Answers (1)

Backlin
Backlin

Reputation: 14842

a cannot be used in the name of the output file as it is not a character variable, it is a data frame.

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")

The syntax of the diag line tends to look funny to new R users, but it is actually just an alternate syntax to call the assignment function diag<-, i.e. diag(x) <- 0 is interpreted as diag<-(x, 0).

Update: Multiple files

If you want to repeat the above for multiple paired files you can do this.

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"))
}

Upvotes: 17

Related Questions