[This email is either empty or too large to be displayed at this time]
Hi, First of all, let me thank you all for replying so rapidly to my first question on this list. It was very very helpfull... and I'm learning R faster and faster. I just encountered a second problem, which may also have a simple solution. Here it is: In my program, a vector is a set of objects. I was looking for a way to store these sets in a big object. I chose to store them in a list. So now I have a list of vectors which looks as follows: [[1]] [1] "a1" "a3" "a4" [[2]] [1] "a1" "a4" "a5" [[3]] [1] "a1" "a5" "a6" Then comes a crucial step where I may have to add the vector ("a3", "a1", "a4") in this list. But as you can see, this set is already present (at position 1 of my list). So it should not be added. If I do a systematic concatenation, at the end, I have a list with too many vectors (where some elements of my list represent the same set). So what I would like to do is a type of Union, but I can't find a way to do it. Unions work only on vectors, and not a vector and a list of vectors. Can anyone help me with this? I thank you very much in advance Patrick
All the ways of doing such things (that I know of) in R only work on atomic objects, so one way to do it is, again, concatenate the vectors into one string, then do the comparison:> lst = list(c("a1", "a3", "a4"), c("a1", "a4", "a5"), c("a1", "a5", "a6")) > lst.vec <- sapply(lst, paste, collapse=":") > lst.vec[1] "a1:a3:a4" "a1:a4:a5" "a1:a5:a6"> x <- lst[[1]] > paste(x, collapse=":") %in% lst.vec[1] TRUE Then you can "grow" the list by something like: if (!paste(x, collapse=":") %in% lst.vec) lst <- c(lst, list(x)) [BTW, note that this only works if order matters; i.e., c("a1", "a3", "a4") not equal to c("a4", "a3", "a1").] HTH, Andy> From: Pat Meyer > > Hi, > > First of all, let me thank you all for replying so rapidly to > my first > question on this list. It was very very helpfull... and I'm > learning R > faster and faster. > > I just encountered a second problem, which may also have a > simple solution. > > Here it is: > > In my program, a vector is a set of objects. > > I was looking for a way to store these sets in a big object. > I chose to > store them in a list. > > So now I have a list of vectors which looks as follows: > > [[1]] > [1] "a1" "a3" "a4" > > [[2]] > [1] "a1" "a4" "a5" > > [[3]] > [1] "a1" "a5" "a6" > > Then comes a crucial step where I may have to add the vector > ("a3", "a1", > "a4") in this list. > > But as you can see, this set is already present (at position > 1 of my list). > So it should not be added. If I do a systematic > concatenation, at the end, I > have a list with too many vectors (where some elements of my > list represent > the same set). > > So what I would like to do is a type of Union, but I can't > find a way to do > it. Unions work only on vectors, and not a vector and a list > of vectors. > > Can anyone help me with this? > > I thank you very much in advance > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
> From: Gabor Grothendieck > > Pat Meyer <paterijk <at> hotmail.com> writes: > > : > : Hi, > : > : First of all, let me thank you all for replying so rapidly > to my first > : question on this list. It was very very helpfull... and I'm > learning R > : faster and faster. > : > : I just encountered a second problem, which may also have a > simple solution. > : > : Here it is: > : > : In my program, a vector is a set of objects. > : > : I was looking for a way to store these sets in a big > object. I chose to > : store them in a list. > : > : So now I have a list of vectors which looks as follows: > : > : [[1]] > : [1] "a1" "a3" "a4" > : > : [[2]] > : [1] "a1" "a4" "a5" > : > : [[3]] > : [1] "a1" "a5" "a6" > : > : Then comes a crucial step where I may have to add the > vector ("a3", "a1", > : "a4") in this list. > : > : But as you can see, this set is already present (at > position 1 of my list). > : So it should not be added. If I do a systematic > concatenation, at the end, I > : have a list with too many vectors (where some elements of > my list represent > : the same set). > : > : So what I would like to do is a type of Union, but I can't > find a way to do > : it. Unions work only on vectors, and not a vector and a > list of vectors. > > L <- list(c("a1","a3","a4"), c("a1","a4","a5"), c("a1","a5","a6")) > newentry <- c("a3", "a1", "a4") > if (!any(sapply(L, setequal, newentry))) L <- c(L, list(newentry))Cool, Gabor! This works even if the sets have the same elements in different orders. Much better than what I had. Best, Andy> ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
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
Initialize the list with the first vector, instead of an emtry list, if you can. Andy> From: Patrick Meyer > > 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Patrick Meyer <patrick.meyer <at> internet.lu> writes: : : 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? : Good point. We can rescue it by doing the simplification ourself using unlist(lapply(...)) instead of sapply(...): L <- list() newentry <- c("a3", "a1", "a4") if (!any(unlist(lapply(L, setequal, newentry)))) L <- c(L, list(newentry))
It does not exactly do what it is meant to... Instead of breaking down, my code is looping forever now... I think I will have a deeper look at it tomorrow... In fact, I have two lists: let's say L and N. An elements (or set) of N must be added to L, if and only if it is not a subset of an element of L. I think that the code you wrote does that, for each element of separately. Couldn't this be a solution: add all the elements of N to L, and afterwards eliminate the sets which are present more than once? How could one eliminate these sets? Thanx for your help... P.