0

I have a list as follows:

mylist <- list(A=seq_len(3)-1, A=seq_len(3)-1, B=seq_len(2)-1, B=seq_len(2)-1)

mylist

$A
[1] 0 1 2

$A
[1] 0 1 2

$B
[1] 0 1

$B
[1] 0 1

meaning that two instances of A can have the state 0,1,2 and two instances of B can have the state 0,1.

I would like to produce strings with products of all combinations of all instances, for which the sum of states is (as an example) 1.

I do this by first getting all possible combinations for both A's and B's and taking the subset for which the sum is 1.

all.combns <- expand.grid(mylist)
ac <- all.combns[which(rowSums(all.combns)==1),]
unname(apply(ac, 1, function(x)paste(colnames(ac), x, sep="_", collapse=" * ")))

Result is:

"A_1 * A_0 * B_0 * B_0" 
"A_0 * A_1 * B_0 * B_0" 
"A_0 * A_0 * B_1 * B_0"
"A_0 * A_0 * B_0 * B_1"

First and second string and third and fourth string are he same. My desired result would be:

"2 * A_1 * A_0 * B_0 * B_0" 
"2 * A_0 * A_0 * B_1 * B_0"

Is there an elegant way to do this? I thought about adding the rows for both A's and B's in all.combns e.g. cbind(all.combns[,1]+all.combns[,2], all.combns[,3]+all.combns[,3]) and then counting the unique elements with methods described here. However, I do think there must be an easier solution to this, without forming all combinations with expand.grid.

Community
  • 1
  • 1
user1981275
  • 13,002
  • 8
  • 72
  • 101
  • @user1987175 -- In regards to your reviewing, this answer here (http://stackoverflow.com/review/low-quality-posts/3871248#./3871248?&_suid=139040787868004590174438491092) is obviously not an answer; please be more careful when reviewing. – LittleBobbyTables - Au Revoir Jan 22 '14 at 16:44

1 Answers1

1

Here is one solution. But I guess there is room for more conciseness. I changed your paste step so the results will be sorted before creating the string as order does not seem to matter in your case. Then use ddply to count identical cases.

ac <- cmbs[rowSums(cmbs) == 1,]
a <- data.frame(v=apply(ac, 1, function(x) 
  paste(sort(paste(colnames(ac), x, sep="_")), collapse=" * ")))
d <- ddply(a, .(v), summarise, new = paste(length(v), "*", unique(v))) 
d[, "new"]

"2 * A_0 * A_0 * B_0 * B_1" "2 * A_0 * A_1 * B_0 * B_0"
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • Thank you for the solution with `plyr`. It works fine for this example, however, I am still thinking of how to avoid using `expand.grid` to calculate all combinations, since in my real application, I have a list with six instances of `A` with values 0 to 4 and six of `B` from 0 to to, which makes 5^6 * 3^6 = 11390625 combinations – user1981275 Jan 13 '14 at 19:20