Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set diagonal of a matrix to zero in R

Tags:

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
like image 930
user1676484 Avatar asked Sep 18 '12 06:09

user1676484


People also ask

Can a diagonal of a matrix be zero?

A square zero matrix is a special diagonal matrix having all its elements equal to zero.

How do I convert a vector to a diagonal matrix in R?

To convert a vector into a diagonal matrix in R, we can use diag function along with matrix function and use ncol argument where we can put the number of columns equal to the number of values in the vector.


1 Answers

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"))
}
like image 175
Backlin Avatar answered Nov 02 '22 17:11

Backlin