I have a file containing, multiple rows as follows
In file1:
a 8|2|3|4 4
b 2|3|5|6|7 5
c 8|5|6|7|9 5
a to a has 4 overlaps, similarly a to b had 2 overlaps, so to check the overlaps between various entity, I need to generate a matrix with the above details, and the output should be a matrix like
a b c
a 4 2 1
b 2 5 3
c 1 3 5
Please give me a suggestion, how to do this? Is there any way to do this using excel or using a shell script or using R? I have written this following code but since I am not a good coder, I couldn't get the output printed in a right format.
setwd('C:\\Users\\Desktop\\')
newmet1<-file("file.txt")
newmet2<-strsplit(readLines(newmet1),"\t")
Newmet<-sapply(newmet2, function(x) x[2:length(x)], simplify=F )
for (i in 1:length(Newmet))
{
for (j in 1:length(Newmet)
{
c <- ((intersect(Newmet[[i]], Newmet[[j]]))
print (length(c))
}
}
Edited: Thanks for all the answers.. I got the matrix using both excel and R with the help of following answers.
Here is a function in R that returns the counts of each columns matches as a new matrix
First we get your data into a R data.frame object:
A <- c(8,2,3,4,NA)
B <- c(2,3,5,6,7)
C <- c(8,5,6,7,9)
dataset <- data.frame(A,B,C)
Then we create a function:
count_matches <- function (x) {
if (is.data.frame(x)) {
y <- NULL
for (i in 1:dim(x)[2]) {
for (j in 1:dim(x)[2]) {
count <- sum(x[[i]][!is.na(x[i])] %in% x[[j]][!is.na(x[j])])
y <- c(y, count)
}
}
y <- matrix(y, dim(x)[2], )
colnames(y) <- names(x)
rownames(y) <- names(x)
return(y)
} else {
print('Argument must be a data.frame')
}
}
We test the function on our dataset:
count_matches(dat)
Which returns a matrix:
A B C
A 4 2 1
B 2 5 3
C 1 3 5
If the numbers are in separate cells starting in Sheet1!A1, try
=SUM(--ISNUMBER(MATCH(Sheet1!$A1:$E1,INDEX(Sheet1!$A$1:$E$3,COLUMN(),0),0)))
starting at Sheet2!A1.
Must be entered as an array formula using Ctrl Shift Enter
Alternative formula that doesn't have to start at Sheet2!A1
SUM(--ISNUMBER(MATCH(Sheet1!$A1:$E1,INDEX(Sheet1!$A$1:$E$3,COLUMNS($A:A),0),0)))
Using R:
# dummy data
df1 <- read.table(text = "a 8|2|3|4 4
b 2|3|5|6|7 5
c 8|5|6|7|9 5", as.is = TRUE)
df1
# V1 V2 V3
# 1 a 8|2|3|4 4
# 2 b 2|3|5|6|7 5
# 3 c 8|5|6|7|9 5
# convert 2nd column to a splitted list
myList <- unlist(lapply(df1$V2, strsplit, split = "|", fixed = TRUE), recursive = FALSE)
names(myList) <- df1$V1
myList
# $a
# [1] "8" "2" "3" "4"
# $b
# [1] "2" "3" "5" "6" "7"
# $c
# [1] "8" "5" "6" "7" "9"
# get overlap counts
crossprod(table(stack(myList)))
# ind
# ind a b c
# a 4 2 1
# b 2 5 3
# c 1 3 5
If we remove data processing bit, this answer is already provided by similar post: Intersect all possible combinations of list elements
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.