Thank you Gabor. But I have a problem with the beginning of my algorithm, where the list you call L is empty... then the code breaks down... It says: "Error in any(...,na.rm = na.rm) : incorrect argument type" How can I handle this? Thank you very much for your help Patrick