Skip to content
Prev 67063 / 398502 Next

lists: removing elements, iterating over elements,

This loop can be replaced with mem <- as.list(1:nItems)...
If I understood the code correctly, this should do the same:

    neighbors <- which(distmat <= radius, arr.ind=TRUE)
    neighbors <- neighbors[neighbors[, 1] != neighbors[, 2],]
    mem <- split(neighbors[, 2], neighbors[, 1])

What I'm not sure of is whether you intend to include the i-th item in the
i-th list (since the distance is presumably 0).  Your code seems to indicate
no, as you have m != n in the if() condition.  The second line above removes
such results.  However, your list below seems to indicate that you do have
such elements in your lists.  If such results can not be in the list, then
the list should already be unique, no?

For deleting an element of a list, see R FAQ 7.1.

HTH,
Andy