I have two questions. I have a dataset named "dt" looks like this:
Name of general security environment health humanrights tradecommerce finance
organization
IRIN 0 1 1 1 1 0 0
IWMI 1 0 1 1 0 0 0
Asyl 0 0 0 0 1 0 0
NetHope 0 0 0 1 0 0 0
USC 0 0 1 0 0 0 0
CIFAL 1 0 1 0 1 0 0
From here, firstly, I would like to know how many times two different rows have, at the same time, a value with "1" within seven columns. For example, IRIN and IWMI have value with "1" two times together on environment and health above. IWMI and NetHope have the value only once together on health. Actually, there are more dozens of rows. I want to know easy-way to get the numbers in R. Again, I want to know how many times two organizations have same value with "1" within the seven columns.
Second, by having the numbers, and then, I would like to create a diagonal matrix based on the numbers that pair organizations have the value with "1" together.
For example, like this:
Name IRIN IWMI Asyl NetHope USC CIFAL
of organization
IRIN 0 2 1 1 1 2
IWMI 2 0 0 1 1 2
Asyl 1 0 0 0 0 1
NetHope 1 1 0 0 0 1
USC 1 1 0 0 0 1
CIFAL 2 2 1 1 1 0
To describe the matrix, as I explained above, IRIN and IWMI had same value "1" twice within the seven columns. That is why there are X12 = 2 as well as X21 = 2 in the 6 * 6 matrix. And, Since IWMI and NetHope have the same value once within the seven columns according to the first dataset above, X24 = 1 and X42 =1 as well. So, I want to know how to make this diagonal matrix based on the numbers.
Overall, as I have over 50 organizations, I would like to know, first, R function or code on how to count number of columns that two different rows have a value with "1" at the same time. Second, once I find the numbers, I would like to know how to make the diagonal matrix based on the number of same value "1" among the seven columns between two organizations.
I hope someone understand this complicated question and have solution.
All the best,
Leroy
Try this:
myfunc <- function(i, j, data, nm = "organization") {
sum(
unlist(subset(data, data[[nm]] == i, select = names(data) != nm)) == 1 &
unlist(subset(data, data[[nm]] == j, select = names(data) != nm)) == 1
)
}
res <- outer(setNames(nm = unique(dat$organization)),
setNames(nm = unique(dat$organization)),
Vectorize(myfunc, vectorize.args = c("i", "j")), data = dat)
diag(res) <- 0 # otherwise, diag(res) is 7, since IRIN==IRIN
res
# IRIN IWMI Asyl NetHope USC CIFAL
# IRIN 0 2 1 1 1 2
# IWMI 2 0 0 1 1 2
# Asyl 1 0 0 0 0 1
# NetHope 1 1 0 0 0 0
# USC 1 1 0 0 0 1
# CIFAL 2 2 1 0 1 0
Data:
dat <- read.table(header=TRUE, text="
organization general security environment health humanrights tradecommerce finance
IRIN 0 1 1 1 1 0 0
IWMI 1 0 1 1 0 0 0
Asyl 0 0 0 0 1 0 0
NetHope 0 0 0 1 0 0 0
USC 0 0 1 0 0 0 0
CIFAL 1 0 1 0 1 0 0 ")