Re: [R] Looping through all possible combinations of cases
Here is another (simpler?) solution: # your 1 column data is actually a vector myvalues - 1:10 names(myvalues) - LETTERS[1:10] # use the QCA package library(QCA) aa - createMatrix(rep(2, length(myvalues))) # set the number of combinations: 2, 3, 4 or whatever combinations - 2 sub.aa - aa[rowSums(aa) == combinations, ] result - apply(sub.aa, 1, function(x) sum(myvalues[x == 1])) names(result) - apply(sub.aa, 1, function(x) paste(names(myvalues)[x == 1], collapse=)) HTH, Adrian On Friday 27 July 2007, Dimitri Liakhovitski wrote: Hello! I have a regular data frame (DATA) with 10 people and 1 column ('variable'). Its cases are people with names ('a', 'b', 'c', 'd', 'e', 'f', etc.). I would like to write a function that would sum up the values on 'variable' of all possible combinations of people, i.e. 1. I would like to write a loop - in such a way that it loops through each possible pair of cases (i.e., ab, ac, ad, etc.) and sums up their respective values on 'variable' 2. I would like to write a loop - in such a way that it loops through each possible trio of cases (i.e., abc, abd, abe, etc.) and sums up their respective values on 'variable'. 3. I would like to write a loop - in such a way that it loops through each possible quartet of cases (i.e., abcd, abce, abcf, etc.) and sums up their respective values on 'variable'. etc. Then, at the end I want to capture all possible combinations that were considered (i.e., what elements were combined in it) and get the value of the sum for each combination. How should I do it? Thanks a lot! Dimitri -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
[R] Looping through all possible combinations of cases
Hello! I have a regular data frame (DATA) with 10 people and 1 column ('variable'). Its cases are people with names ('a', 'b', 'c', 'd', 'e', 'f', etc.). I would like to write a function that would sum up the values on 'variable' of all possible combinations of people, i.e. 1. I would like to write a loop - in such a way that it loops through each possible pair of cases (i.e., ab, ac, ad, etc.) and sums up their respective values on 'variable' 2. I would like to write a loop - in such a way that it loops through each possible trio of cases (i.e., abc, abd, abe, etc.) and sums up their respective values on 'variable'. 3. I would like to write a loop - in such a way that it loops through each possible quartet of cases (i.e., abcd, abce, abcf, etc.) and sums up their respective values on 'variable'. etc. Then, at the end I want to capture all possible combinations that were considered (i.e., what elements were combined in it) and get the value of the sum for each combination. How should I do it? Thanks a lot! Dimitri __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looping through all possible combinations of cases
Lots of ways. Here is a simpler one. start by reading ?combn ?subset ?Subscript ?is.element ?apply ?paste - note the 'collapse' arg ?names - note the 'names(x) - value' usage ?list ?unlist then write a function that calc's the sum of variable given a vector of names, then figure out how to use apply on the result of combn() to feed a vector of names to that function, then figure out how to use paste() to turn a vector into a single string, then figure out how to use apply() with paste() to turn the vectors of names into labels (like 'a:b' ) and 'names-' to attach them to the result of the earlier apply, and finally wrap it all into a loop (for (i in 2:9) {...} saving the results as res[[i]] - value at teh end of each loop. At the end 'unlist(res)' will produce a named vector of the sums with each name indicating the people who contributed to it. If you get stuck along the way report back to the list AFTER following the suggestions in the POSTING GUIDE mentioned at the bottom of this email. On Fri, 27 Jul 2007, Dimitri Liakhovitski wrote: Hello! I have a regular data frame (DATA) with 10 people and 1 column ('variable'). Its cases are people with names ('a', 'b', 'c', 'd', 'e', 'f', etc.). I would like to write a function that would sum up the values on 'variable' of all possible combinations of people, i.e. 1. I would like to write a loop - in such a way that it loops through each possible pair of cases (i.e., ab, ac, ad, etc.) and sums up their respective values on 'variable' 2. I would like to write a loop - in such a way that it loops through each possible trio of cases (i.e., abc, abd, abe, etc.) and sums up their respective values on 'variable'. 3. I would like to write a loop - in such a way that it loops through each possible quartet of cases (i.e., abcd, abce, abcf, etc.) and sums up their respective values on 'variable'. etc. Then, at the end I want to capture all possible combinations that were considered (i.e., what elements were combined in it) and get the value of the sum for each combination. How should I do it? Thanks a lot! Dimitri __ R-help@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 and provide commented, minimal, self-contained, reproducible code. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looping through all possible combinations of cases
Here is how to do it for 2; you can extend it: # test data n - 100 x - data.frame(id=sample(letters[1:4], n, TRUE), values=runif(n)) # get combinations of 2 at a time comb.2 - combn(unique(as.character(x$id)), 2) for (i in 1:ncol(comb.2)){ + cat(sprintf(%s:%s %f\n,comb.2[1,i], comb.2[2,i], + sum(x$value[x$id %in% comb.2[,i]]))) + } c:d 25.259988 c:b 21.268737 c:a 21.250933 d:b 26.013253 d:a 25.995450 b:a 22.004198 On 7/27/07, Dimitri Liakhovitski [EMAIL PROTECTED] wrote: Hello! I have a regular data frame (DATA) with 10 people and 1 column ('variable'). Its cases are people with names ('a', 'b', 'c', 'd', 'e', 'f', etc.). I would like to write a function that would sum up the values on 'variable' of all possible combinations of people, i.e. 1. I would like to write a loop - in such a way that it loops through each possible pair of cases (i.e., ab, ac, ad, etc.) and sums up their respective values on 'variable' 2. I would like to write a loop - in such a way that it loops through each possible trio of cases (i.e., abc, abd, abe, etc.) and sums up their respective values on 'variable'. 3. I would like to write a loop - in such a way that it loops through each possible quartet of cases (i.e., abcd, abce, abcf, etc.) and sums up their respective values on 'variable'. etc. Then, at the end I want to capture all possible combinations that were considered (i.e., what elements were combined in it) and get the value of the sum for each combination. How should I do it? Thanks a lot! Dimitri __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@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 and provide commented, minimal, self-contained, reproducible code.