[R] how to exclude rows with not-connected coalitions
Petr Savicky
savicky at cs.cas.cz
Thu Feb 9 13:44:17 CET 2012
On Thu, Feb 09, 2012 at 12:18:06PM +0000, Schumacher, G. wrote:
> Dear all,
>
> I have question but cannot explain without providing some context first:
>
> I want to calculate how many policy-connected coalitions between 7 parties are possible. I have positions on an one-dimensional scale for each party and I have sorted the parties on the positions (it is sorted from extreme left to extreme right, hence using a left-right scale). A policy-connected coalition consists of parties that are connected on this left-right scale, hence there cannot be a party outside the coalition that is in-between two parties in the coalition on the left-right scale. My question is: how do a make a matrix that excludes those coalitions that are not policy-connected.
>
> I made a matrix with each combination of 0 (out of coalition) and 1 (in coalition), for 7 parties.
>
> a <- expand.grid(rep(list(c(0,1)),7))
>
> a gives all possible coalitions between these 7 parties.
>
> Now I want to exclude those rows with coalition that are not policy-connected.
>
> Hence: this one should be out, because the fifth party is not in.
>
> 0 1 1 1 0 1 1
>
> And this one should be in,
>
> 0 0 0 1 1 1 1.
Hi.
If i understand correctly, a connected coalition is
an interval on your scale. If this is correct, try
the following for generating only the intervals.
n <- 7
a <- matrix(nrow=n+choose(n, 2), ncol=n)
k <- 1
for (i in 1:n) {
for (j in (i+1):(n+1)) {
x <- rep(0, times=n+1)
x[i] <- 1
x[j] <- 1
a[k,] <- cumsum(x)[1:n]
k <- k+1
}
}
a[a == 2] <- 0
a
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 0 0 0 0 0 0
[2,] 1 1 0 0 0 0 0
[3,] 1 1 1 0 0 0 0
[4,] 1 1 1 1 0 0 0
[5,] 1 1 1 1 1 0 0
[6,] 1 1 1 1 1 1 0
[7,] 1 1 1 1 1 1 1
[8,] 0 1 0 0 0 0 0
[9,] 0 1 1 0 0 0 0
[10,] 0 1 1 1 0 0 0
...
If you already have the matrix of all coalitions
and want to detect the connected ones, try the
function ?rle. There should be exactly one
run of ones.
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list