I need to find whether is there an intersection between rows and col names in a matrix. Prior to counting on whether there is a match I need to split each label according to a pattern.
Thanks in advance
#ad hoc function
count_intersect <- function(x,y,symbol){
require(stringr)
x <- strsplit(x = x,split = symbol, perl = T)
y <- strsplit(x = y,split = symbol, perl = T)
result <- ifelse(length(intersect(x,y)),1,0)
return(result)
}
rows <- c("a b","a c","a")
cols <- c("a","a c","d")
#my attempt
outer(rows, cols, count_intersect(""))
#toy example
expected = matrix(data = c(1,1,1,1,1,1,0,0,0),nrow = 3, ncol = 3)
rownames(expected) <- c("a b","a c","a")
colnames(expected) <- c("a","a c","d")
a a c d
a b 1 1 0
a c 1 1 0
a 1 1 0