Entering edit mode
8.3 years ago
star
▴
350
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