r - Expand grid of all possible combinations within groups -


i facing following problem. have list of m&a transactions, each transactions includes data on (1) acquirer, (2) vendor, (3) target. data structured in way relationship can n:n:n , looks similar following:

dealid acquirer target vendor 1      firma    firmb  firmc 1      firmd           firme 2      ..................... 

so problem rows within deal have no meaning per se, e.g., firmd co-acquirer firmb.

i need create possible acquirer-target-vendor combinations within each dealid. have managed expand grids expand.grid function or via merge. however, not know how expand grid of possible combinations within groups.

you dplyr , expand tidyr.

df <- read.table(text="dealid acquirer target vendor 1      firma    firmb  firmc 1      firmd    na     firme 2      firma    na     firmc 2      firmd    na     firme 2      firmg    firmf  firme",header=true,stringsasfactors=false)  library(dplyr);library(tidyr) df%>%   group_by(dealid)%>%   expand(acquirer, target, vendor)     dealid acquirer target vendor     <int>    <chr>  <chr>  <chr>  1      1    firma  firmb  firmc  2      1    firma  firmb  firme  3      1    firmd  firmb  firmc  4      1    firmd  firmb  firme  5      2    firma  firmf  firmc  6      2    firma  firmf  firme  7      2    firmd  firmf  firmc  8      2    firmd  firmf  firme  9      2    firmg  firmf  firmc 10      2    firmg  firmf  firme 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -