Dimitri Liakhovitski
2007-Jul-27 18:35 UTC
[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
Petr PIKAL
2007-Jul-30 14:02 UTC
[R] Odp: Looping through all possible combinations of cases
Hi I am not sure about using loops but something like colSums(combn(DATA[,1],2)) gives you sums of all pairs and colSums(combn(DATA[,1],3)) sums of all triplets etc. if you want to know which ones they are just use combn on names. e.g.> yX1.5 a 1 b 2 c 3 d 4 e 5> colSums(combn(y[,1],2))[1] 3 4 5 6 5 6 7 7 8 9> combn(row.names(y),2)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d" [2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e" Regards Petr r-help-bounces at stat.math.ethz.ch napsal dne 27.07.2007 20:35:39:> 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 at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry
2007-Jul-30 16:23 UTC
[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 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
> and provide commented, minimal, self-contained, reproducible code.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
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 <ld7631 at gmail.com> 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 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 > 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?
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