[R] Putting x into the right subset

John Wiedenhoeft wiedenhoeft at gmx.net
Sat Jul 22 15:21:31 CEST 2006

```Dear Uwe,

exactly what I need (I slightly modified it, but I don't think that
caused the problem):

M <- list()
temp <- NA
for (i in 1:length(deppat))
{
a <- deppat[i]
b <- deptest[i]
x <- c(a, b)
if (i > 1)
temp <- which(sapply(M, function(y) any(x %in% y)))[1]
if(!is.na(temp))
M[[temp]] <- c(M[[temp]], x)
else
M[[length(M) + 1]] <- x
}

print(M)
print(length(M))
for (k in 1:length(M))
{
M[[k]] <- sort(unique(M[[k]]))
print(M[[k]])
}

deppat and deptest are equal in length. Additionaly, they may contain
values more than once. At the end, M shall contain exactly
length(unique(c(deppat, deptest))) variables, but it doesn't. Also,
there are some matches that remained undetected:

M[[1]] 1  5  6 11 22 24 28 29 31 35 49 57 58 62 65 75 80 81 85 86
M[[2]] 2  7 11 12 25 30 31 32 36 50 53 58 59 63 66 69 82 87

11, 31 and 58 occur in two subsets (there are more examples for larger
k).

I've tried to do it more graphic, but for some reason the same failure
occures:

M <- list()
for (j in 1:length(deppat))
{
for (listpos in 1:(length(M)+1))
{
if (listpos > length(M))
{
M[[listpos]] <- c(deppat[j], deptest[j])
break
}
else
{
if (deppat[j] %in% M[[listpos]] || deptest[j] %in%
M[[listpos]])
{
M[[listpos]] <- c(M[[listpos]], deppat[j], deptest[j])
break
}
}
}
}
print(M)

for (k in 1:length(M))
{
M[[k]] <- sort(unique(M[[k]]))
print(M[[k]])
}

There must be an error in reasoning, but I can't figure out where it
is...

Cheers,
John

```