Schumacher, G.
2012-Feb-09 12:18 UTC
[R] how to exclude rows with not-connected coalitions
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. Hope this is clear and that someone has a good suggestion. [[alternative HTML version deleted]]
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.
ONKELINX, Thierry
2012-Feb-09 13:09 UTC
[R] how to exclude rows with not-connected coalitions
This can be done without nested loops. a <- expand.grid(rep(list(c(0,1)),7)) tmp <- apply(a, 1, function(x){ var(cumsum(x == 0)[x == 1]) }) a[is.na(tmp) | tmp == 0, ] ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Petr Savicky Verzonden: donderdag 9 februari 2012 13:44 Aan: r-help at r-project.org Onderwerp: Re: [R] how to exclude rows with not-connected coalitions 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. ______________________________________________ R-help at r-project.org 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.
Schumacher, G.
2012-Feb-09 13:21 UTC
[R] how to exclude rows with not-connected coalitions
Thanks Thierry and Petr for your solutions. I can continue now (until the next problem arises ;). ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf of ONKELINX, Thierry [Thierry.ONKELINX at inbo.be] Sent: Thursday, February 09, 2012 2:09 PM To: Petr Savicky; r-help at r-project.org Subject: Re: [R] how to exclude rows with not-connected coalitions This can be done without nested loops. a <- expand.grid(rep(list(c(0,1)),7)) tmp <- apply(a, 1, function(x){ var(cumsum(x == 0)[x == 1]) }) a[is.na(tmp) | tmp == 0, ] ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Petr Savicky Verzonden: donderdag 9 februari 2012 13:44 Aan: r-help at r-project.org Onderwerp: Re: [R] how to exclude rows with not-connected coalitions 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. ______________________________________________ R-help at r-project.org 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-help at r-project.org 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.