This is a test version of Biostars. For the public version, visit https://www.biostars.org.
how compare rows together?

I have data like as below:

I want to compare each row with rows below and then get number of equal value in each 2 rows.

   V1  V2  V3   V4  V5
1   3   2   2   3   3
2   2   2   0   2   3
3   2   3   2   2   0
4   0   3   2   1   3
5   2   2   2   2   3

I wrote the code below, but in output I face with NA.

library(data.table)
data=as.data.frame(read.table("data.csv",header=TRUE))

findMatch <- function(i,n){
  tmp <- colSums(t(data[-(1:i),]) == unlist(data[i,]))
  tmp <- tmp[tmp > n]
  if(length(tmp) > 0)return(data.table(sample=rownames(data[i],duplicate=names(tmp),match=tmp))
  return(NULL)
}
tab <- rbindlist(lapply(1:(nrow(data)-1),findMatch,n=0))
tab

Output:

       sample duplicate match
 1:      1        NA    NA
 2:      1        NA    NA
 3:      1        NA    NA
 4:      1        NA    NA

I expect this out put:

       sample duplicate match
 1:      1        2    2
 2:      1        3    1
 3:      1        4    2
 4:      1        5    3
r bioconductor statistics

Hello star!

We believe that this post does not fit the main topic of this site.

Not sufficiently relevant for bioinformatics, try stackoverflow for example.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

0 answers

No answers yet.

Log in to answer this question.