I have a list with several elements (up to 45) . There are some rows which are equal between some elements : I would like to gather these elements and then, remove the redundant rows in order to keep only one row from redundant rows.
This is a reproducible example (this is the file data
):
OTU0001 OTU0004 OTU0014 OTU0016 OTU0017 OTU0027 OTU0029 OTU0030
Sample_10.rare 1 1 86 1 1 1 1 1
Sample_11.rare 1 43 170 1 43 128 1 86
Sample_12.rare 1 1 1 1 1 1 1 43
Sample_13.rare 763 551 2160 128 551 1 678 1398
ncol=ncol(data)
rest<-ncol%%2
blocks<-ncol%/%2
ngroup <- rep(1:blocks, each = 2)
split <- split(1:ncol,ngroup)
combs <- expand.grid(1:length(split), 1:length(split))
combs <- t(apply(combs, 1, sort))
combs <- unique(combs)
combs <- combs[combs[,1] != combs[,2],]
cor_rho<-function(y) {
resMAT <- foreach(i = seq_len(ncol(y)),
.combine = rbind,
.multicombine = TRUE,
.inorder = FALSE,
.packages = c('data.table', 'doParallel')) %dopar% {
apply(y, 2, function(x) 1 - ((var(y[,i] - x)) / (var(y[,i]) + var(x))))}
colnames(resMAT)=rownames(resMAT)=colnames(y)
Df<-data.frame(var1=rownames(resMAT)[row(resMAT)[upper.tri(resMAT)]],
var2=colnames(resMAT)[col(resMAT)[upper.tri(resMAT)]],
corr=resMAT[upper.tri(resMAT)])
return(Df)}
res <- foreach(i = seq_len(nrow(combs))) %dopar% {
G1 <- split[[combs[i,1]]]
G2 <- split[[combs[i,2]]]
dat.i <- cbind(data[,G1], data[,G2])
rho.i <- cor_rho(dat.i)
}
res #I get my list
[[5]]
var1 var2 corr
1 OTU0014 OTU0016 0.1214562
2 OTU0014 OTU0029 0.5875550
3 OTU0016 OTU0029 0.3624304
4 OTU0014 OTU0030 0.9136386
5 OTU0016 OTU0030 0.1853840
6 OTU0029 OTU0030 0.7980875
[[6]]
var1 var2 corr
1 OTU0017 OTU0027 -0.11770325
2 OTU0017 OTU0029 0.97129390
3 OTU0027 OTU0029 -0.12081013
4 OTU0017 OTU0030 0.68441352
5 OTU0027 OTU0030 -0.05400953
6 OTU0029 OTU0030 0.79808749
Thanks