product combinations sold in each store

时间:2019-01-15 18:09:25

标签: r

I have data which looks like the following:

1 " Chain42 "     4  2008 BUD LIGHT CHELADA                22
2 " Chain48 "    12  2005 TEQUIZA                           1
3 " Chain43 "     3  2012 MICHELOB ULTR POMEGRANAT RSPB    13
4 " Chain96 "     5  2002 DOC OTIS HARD LEMON               1
5 " Chain49 "     2  2007 BUD LIGHT                        33
6 " Chain52 "     1  2003 BUDWEISER                       184

I am trying to create another table which will give me all the combinations common to each "Chain".

i.e. "BUDWEISER" was sold in "Chain52" and it was also sold in "Chain67".

"BUD LIGHT" was sold in "Chain49" and it was also sold in "Chain52".

I am trying to create a matrix of firstly zero and 1`s if the same product was sold in a give store.

i.e. create a list or a number of data frames (1 for each "product") and a grid like the following

Bud Light & Budweiser

                 Chain1     Chain2        Chain3   Chain4

          Chain1   -                       
Budweiser Chain2   1          -             
          Chain3   0          1            -  
          Chain4   0          1            1           -

For the number of units sold:

                 Chain1     Chain2        Chain3   Chain4

          Chain1   -                       
Bud Light Chain2   234        -             
          Chain3   72        541          -  
          Chain4   0         231            0           -

and I want to do this for all possible product combinations.

Secondly I would like to do the same thing but instead of zeros and 1s put the numbern`

(If you can suggest a "better" way to represent the data then I am open to ideas also)

#

DATA:

structure(list(MskdName = structure(c(66L, 72L, 67L, 137L, 73L, 
76L, 68L, 86L, 134L, 89L, 103L, 86L, 94L, 134L, 107L, 66L, 95L, 
73L, 85L, 66L, 12L, 68L, 3L, 90L, 121L, 103L, 80L, 138L, 93L, 
90L, 94L, 73L, 94L, 67L, 135L, 90L, 75L, 106L, 41L, 72L, 76L, 
89L, 66L, 89L, 80L, 72L, 111L, 103L, 89L, 12L, 121L, 136L, 66L, 
85L, 86L, 90L, 86L, 85L, 76L, 41L, 67L, 93L, 7L, 78L, 134L, 67L, 
90L, 89L, 43L, 6L, 90L, 134L, 72L, 67L, 71L, 78L, 86L, 135L, 
75L, 12L, 103L, 134L, 86L, 103L, 41L, 95L, 71L, 6L, 73L, 75L, 
9L, 41L, 41L, 68L, 93L, 90L, 67L, 67L, 71L, 111L), .Label = c(" Chain1  ", 
" Chain10 ", " Chain100", " Chain101", " Chain102", " Chain103", 
" Chain104", " Chain105", " Chain106", " Chain107", " Chain108", 
" Chain109", " Chain11 ", " Chain110", " Chain112", " Chain113", 
" Chain114", " Chain115", " Chain116", " Chain117", " Chain118", 
" Chain119", " Chain120", " Chain122", " Chain124", " Chain125", 
" Chain126", " Chain127", " Chain128", " Chain129", " Chain13 ", 
" Chain130", " Chain131", " Chain132", " Chain133", " Chain134", 
" Chain135", " Chain136", " Chain137", " Chain14 ", " Chain15 ", 
" Chain16 ", " Chain17 ", " Chain18 ", " Chain20 ", " Chain22 ", 
" Chain23 ", " Chain24 ", " Chain25 ", " Chain27 ", " Chain28 ", 
" Chain29 ", " Chain3  ", " Chain30 ", " Chain31 ", " Chain32 ", 
" Chain33 ", " Chain34 ", " Chain35 ", " Chain36 ", " Chain37 ", 
" Chain39 ", " Chain4  ", " Chain40 ", " Chain41 ", " Chain42 ", 
" Chain43 ", " Chain44 ", " Chain45 ", " Chain46 ", " Chain47 ", 
" Chain48 ", " Chain49 ", " Chain5  ", " Chain50 ", " Chain52 ", 
" Chain53 ", " Chain54 ", " Chain55 ", " Chain57 ", " Chain58 ", 
" Chain59 ", " Chain6  ", " Chain60 ", " Chain61 ", " Chain62 ", 
" Chain63 ", " Chain64 ", " Chain65 ", " Chain66 ", " Chain69 ", 
" Chain7  ", " Chain70 ", " Chain71 ", " Chain72 ", " Chain73 ", 
" Chain74 ", " Chain75 ", " Chain76 ", " Chain77 ", " Chain78 ", 
" Chain79 ", " Chain8  ", " Chain80 ", " Chain81 ", " Chain83 ", 
" Chain84 ", " Chain85 ", " Chain86 ", " Chain87 ", " Chain88 ", 
" Chain89 ", " Chain9  ", " Chain90 ", " Chain91 ", " Chain92 ", 
" Chain94 ", " Chain95 ", " Chain97 ", " Chain98 ", " Chain99 ", 
" NONE    ", " Chain111", " Chain12 ", " Chain121", " Chain123", 
" Chain19 ", " Chain2  ", " Chain21 ", " Chain26 ", " Chain38 ", 
" Chain51 ", " Chain56 ", " Chain67 ", " Chain68 ", " Chain82 ", 
" Chain96 ", " Chain93 "), class = "factor"), month = c(4, 12, 
3, 5, 2, 1, 6, 1, 4, 9, 7, 6, 3, 2, 8, 3, 11, 5, 9, 5, 5, 10, 
2, 9, 12, 9, 3, 8, 8, 9, 9, 10, 11, 2, 12, 1, 4, 5, 11, 10, 6, 
4, 11, 2, 11, 12, 11, 2, 1, 9, 6, 5, 7, 5, 9, 3, 3, 10, 3, 8, 
8, 11, 5, 12, 12, 1, 8, 12, 11, 12, 12, 6, 9, 7, 9, 10, 6, 8, 
11, 6, 11, 12, 12, 5, 10, 12, 12, 3, 10, 4, 8, 1, 1, 8, 5, 4, 
12, 7, 1, 8), year = c(2008, 2005, 2012, 2002, 2007, 2003, 2010, 
2010, 2004, 2007, 2004, 2010, 2009, 2012, 2002, 2008, 2002, 2005, 
2012, 2008, 2001, 2010, 2003, 2005, 2007, 2003, 2001, 2003, 2003, 
2011, 2009, 2005, 2009, 2011, 2010, 2011, 2004, 2012, 2012, 2007, 
2003, 2007, 2008, 2009, 2001, 2006, 2009, 2005, 2007, 2001, 2001, 
2003, 2008, 2011, 2010, 2008, 2010, 2011, 2003, 2011, 2011, 2003, 
2003, 2001, 2012, 2011, 2008, 2007, 2009, 2001, 2010, 2012, 2006, 
2011, 2009, 2002, 2010, 2010, 2004, 2001, 2003, 2004, 2010, 2003, 
2011, 2002, 2009, 2002, 2007, 2004, 2001, 2012, 2011, 2010, 2003, 
2005, 2012, 2012, 2009, 2009), L5 = structure(c(104L, 49L, 121L, 
18L, 8L, 9L, 30L, 105L, 31L, 38L, 43L, 45L, 13L, 9L, 44L, 93L, 
15L, 20L, 42L, 128L, 13L, 43L, 49L, 31L, 16L, 41L, 18L, 6L, 30L, 
31L, 147L, 30L, 47L, 47L, 9L, 16L, 42L, 8L, 15L, 7L, 7L, 98L, 
104L, 94L, 45L, 43L, 8L, 7L, 38L, 8L, 38L, 6L, 94L, 146L, 128L, 
26L, 13L, 102L, 15L, 43L, 123L, 32L, 16L, 31L, 43L, 105L, 47L, 
16L, 43L, 26L, 41L, 209L, 26L, 42L, 122L, 6L, 77L, 121L, 55L, 
25L, 31L, 57L, 124L, 37L, 15L, 37L, 94L, 26L, 57L, 44L, 30L, 
8L, 15L, 29L, 47L, 31L, 43L, 215L, 115L, 115L), .Label = c("ALL ANHEUSER BUSCH PRODUCTS", 
"ALL BUDWEISER PRODUCTS", "ALL BUSCH PRODUCTS", "ALL MICHELOB PRODUCTS", 
"BDWSR BUDLT", "BUD ICE", "BUD ICE LIGHT", "BUD LIGHT", "BUDWEISER", 
"BUDWEISER DRY", "BUDWEISER ICE DRAFT", "BUDWEISER MILLENNIUM", 
"BUSCH", "BUSCH ICE", "BUSCH LIGHT", "BUSCH NA", "CZECHVAR", 
"DOC OTIS HARD LEMON", "GROLSCH AMBER ALE", "GROLSCH LAGER", 
"GROLSCH SUMMER BLONDE", "GROLSCH VARIETY PACK", "HURRICANE ICE", 
"HURRICANE MALT LIQUOR", "KILLARNEYS RED LAGER", "KING COBRA", 
"KIRIN ICHIBAN", "KIRIN LAGER", "KIRIN LIGHT", "MICHELOB", "MICHELOB AMBER BOCK", 
"MICHELOB BLACK AND TAN", "MICHELOB DRY", "MICHELOB GOLDEN DRAFT", 
"MICHELOB GOLDEN DRAFT LIGHT", "MICHELOB HEFEWEIZEN", "MICHELOB HONEY LAGER", 
"MICHELOB LIGHT", "MICHELOB PALE ALE", "MICHELOB SPCLTY ALES & LGRS W", 
"MICHELOB ULTRA", "NATURAL ICE", "NATURAL LIGHT", "ODOULS", "ODOULS AMBER", 
"RED WOLF", "ROLLING ROCK EXTRA PALE", "ROLLING ROCK LIGHT", 
"TEQUIZA", "TEQUIZA EXTRA", "ZIEGENBOCK AMBER", "BACARDI SILVER", 
"MICHELOB GOLDEN PILSNER", "MICHELOB SPECIALTY ALES & LAG", "ANHEUSER WORLD SELECT", 
"BACARDI SILVER O3", "BACARDI SILVER RAZ", "MICHELOB CLASSIC DARK", 
"TIGER LAGER", "ZIEGEN LIGHT", "B E", "BACARDI SILVER LIMON", 
"BACARDI SILVER SAMPLER", "BUDWEISER SELECT", "9TH STREET MARKET LIME CACTUS", 
"9TH STRT MARKT BLD ORANG GRAP", "9TH STRT MARKT POMEGRANT RSPB", 
"ANHEUSER WORLD LAGER", "BLUE HORIZON", "BREWMASTERS PRIVATE RESERVE", 
"GROLSCH LIGHT LAGER", "HURRICANE HIGH GRAVITY", "MICHELOB CELEBRATE", 
"MICHELOB ULTRA AMBER", "PEELS", "TILT", "WILD BLUE", "BARE KNUCKLE STOUT", 
"BDDRY BDWSR BUDLT", "BUDWEISER HAPPY HOLIDAYS", "HARBIN LAGER", 
"JACKS PUMPKIN SPICE ALE", "MICHELOB CELEBRATE GIFT PACK", "PEELS SPICED APPLE", 
"PEELS VARIETY PACK", "REDBRIDGE", "SPYKES", "STONE MILL", "TILT 8 PERCENT", 
"WILD HOP", "WINTER BOURBON CASK ALE", "ABBEY DE LEFFE BLONDE PALE AL", 
"BACARDI SILVER MOJITO", "BASS PALE ALE", "BEACH BUM BLONDE ALE", 
"BECKS", "BECKS AND BECKS LIGHT", "BECKS DARK", "BECKS OKTOBERFEST", 
"BECKS PREMIER LIGHT", "BEST OF BELGIUM", "BODDINGTONS PUB CREAM ALE", 
"BUD EXTRA", "BUD LIGHT CHELADA", "BUDWEISER CHELADA", "GROLSCH SAMPLER PACK", 
"HAAKE BECK NA", "HOEGAARDEN WHITE ALE", "HURRICANE HIGH GRAVITY LAGER", 
"INTENSITEA LEMON", "INTENSITEA PEACH", "INTENSITEA RASPBERRY", 
"INTENSITEA SAMPLER", "INTENSITEA VARIETY PACK", "LANDSHARK LAGER", 
"LOWENBRAU", "LOWENBRAU OKTOBERFEST", "MICHELOB BAVARIAN STYLE WHEAT", 
"MICHELOB CELEBRATE CHOCOLATE", "MICHELOB MARZEN", "MICHELOB ULTR POMEGRANAT RSPB", 
"MICHELOB ULTR TUSCN ORNG GRAP", "MICHELOB ULTRA FRUIT SAMPLER", 
"MICHELOB ULTRA LIME CACTUS", "MXOLOGI", "RAY HILLS AMERICAN PILSNER", 
"ROCK GREEN LIGHT", "SHOCK TOP BELGIAN WHITE ALE", "SPRING HEAT SPICED WHEAT", 
"SPYKES SAMPLER PACK", "STAROPRAMEN", "STELLA ARTOIS LAGER", 
"TAKE 6 HOME", "TASTE OF ASIA", "TENNENTS LAGER", "BACARDI SILVER MOJITO PARTY P", 
"BRAHMA", "BUD LIGHT LIME", "BUDWEISER AMERICAN ALE", "KOKANEE GLACIER", 
"KOKANEE GOLD", "MACKESON STOUT", "MACKESON TRIPLE XXX STOUT", 
"MICHELOB CELEBRATE CHERRY LAG", "MICHELOB DUNKEL WEISSE", "MICHELOB SEASONAL", 
"ROCK LIGHT", "ROLLING ROCK RED", "WHITBREAD TRADITIONAL PALE AL", 
"WILD HOP ORGANIC LAGER", "BACARDI SILVER SIGNATURE", "BUD LIGHT GOLDEN WHEAT", 
"BUDWEISER SELECT 55", "MICHELOB IRISH RED ALE", "MICHELOB PORTER", 
"MICHELOB SEASONAL SAMPLER PAC", "MICHELOB SPRING SAMPLER PACK", 
"BUD LIGHT PARTY PACK", "MICHELOB ULTRA DRAGON FRUIT P", "ABBEY DE LEFFE BROWN ALE", 
"ALEXANDER KEITHS BROWN ALE", "ALEXANDER KEITHS LAGER", "ALEXANDER KEITHS PALE ALE", 
"GOOSE ISLAND MATILDA", "GOOSE ISLAND PEPE NERO ALE", "GOOSE ISLND KNG HNRY BRLY WN", 
"MARGARITAVILLE", "NATTY DADDY LAGER", "ROLLING ROCK", "SHOCK TOP PUMPKIN WHEAT", 
"SHOCK TOP RASPBERRY WHEAT ALE", "SHOCK TOP VARIETY PACK", "STAROPRAMEN PREMIUM LAGER", 
"ABBEY DE LEFFE BRUNE ALE", "ALEXANDER KEITHS VARIETY PACK", 
"BACARDI PLUS ARCTIC GRAPE", "BACARDI PLUS TORCHED CHERRY", "BASS BLACK AND TAN PUB KIT", 
"BASS IPA", "BECKS SAPPHIRE LAGER", "BUD LIGHT LIME LIME A RITA", 
"BUD LIGHT PLATINUM LAGER", "BUDWEISER BLCK CRWN GLDN AMB", "BUDWEISER BREWMASTERS PROJECT", 
"BUDWEISER CHELADA PICANTE", "FRANZISKANER HEFE", "GOOSE ISLAND 312 URBAN WHEAT", 
"GOOSE ISLAND BIG JOHN IMPERL", "GOOSE ISLAND BOURBON COUNTY S", 
"GOOSE ISLAND CHRISTMAS ALE", "GOOSE ISLAND DEMOLITION ALE", 
"GOOSE ISLAND FLR BELGN STYL A", "GOOSE ISLAND HONKERS ALE", 
"GOOSE ISLAND IPA", "GOOSE ISLAND NUT BROWN ALE", "GOOSE ISLAND PERE JACQUES ALE", 
"GOOSE ISLAND PUB PACK", "GOOSE ISLAND SEASONAL", "GOOSE ISLAND SOFI BELGN STYL", 
"GOOSE ISLND BOURBN CNTY COFF", "GOOSE ISLND BRBN CNTY CHRRY R", 
"GOOSE ISLND JULT BLGN STYL WL", "GOOSE ISLND LOLT BLGN STYL WL", 
"GOOSE ISLND MADM ROS BLGN STY", "GOOSE ISLND NGHT STALKR IMPRL", 
"KIRIN ICHIBAN LAGER", "MARGARITAVILLE 5 O CLOCK", "MARGARITAVILLE ISLAND BEACH B", 
"MICHELOB ULTRA LIGHT", "PRESIDENTE", "PRESIDENTE LIGHT", "RASCALS WILD RED LAGER", 
"ROLLING ROCK GREEN LIGHT", "SAINT PAULI GIRL", "SAINT PAULI GIRL DARK", 
"SAINT PAULI N A", "SHADOWS WILD BLACK LAGER", "SHOCK TOP LEMON SHANDY WHEAT", 
"SHOCK TOP SEASONAL", "SHOCK TOP WHEAT IPA", "TILT HARD LEMON", 
"ULTRA 19TH HOLE"), class = "factor"), n = c(22L, 1L, 13L, 1L, 
33L, 184L, 13L, 4L, 39L, 43L, 17L, 11L, 18L, 27L, 3L, 34L, 52L, 
16L, 17L, 13L, 17L, 129L, 7L, 35L, 4L, 20L, 13L, 5L, 44L, 1L, 
2L, 46L, 6L, 48L, 26L, 8L, 55L, 42L, 2L, 36L, 54L, 23L, 25L, 
21L, 15L, 90L, 53L, 4L, 52L, 29L, 2L, 3L, 26L, 1L, 10L, 3L, 67L, 
6L, 149L, 5L, 4L, 8L, 2L, 8L, 12L, 4L, 5L, 20L, 4L, 1L, 4L, 3L, 
4L, 80L, 14L, 63L, 15L, 3L, 10L, 2L, 4L, 15L, 4L, 3L, 9L, 7L, 
10L, 3L, 5L, 36L, 41L, 15L, 9L, 1L, 34L, 36L, 118L, 10L, 40L, 
1L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-100L), vars = c("MskdName", "month", "year"), drop = TRUE, indices = list(
    22L, 87L, 69L, 62L, 90L, 20L, 79L, 49L, 92L, 91L, 59L, 84L, 
    38L, 68L, 15L, 0L, 19L, 52L, 42L, 65L, 33L, 2L, 73L, 97L, 
    60L, 96L, 6L, 93L, 21L, 98L, 74L, 86L, 72L, 39L, 1L, 45L, 
    4L, 17L, 31L, 88L, c(36L, 89L), 78L, 5L, 58L, 40L, 75L, 63L, 
    26L, 44L, 53L, 18L, 57L, 7L, 56L, c(11L, 76L), 54L, 82L, 
    48L, 43L, 41L, 9L, 67L, 35L, 55L, 95L, 66L, 23L, 29L, 70L, 
    94L, 28L, 61L, 12L, 30L, 32L, 16L, 85L, 47L, 83L, 10L, 25L, 
    80L, 37L, 14L, 99L, 46L, 50L, 24L, 13L, 8L, 71L, 81L, 64L, 
    77L, 34L, 51L, 3L, 27L), group_sizes = c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 2L, labels = structure(list(
    MskdName = structure(c(3L, 6L, 6L, 7L, 9L, 12L, 12L, 12L, 
    41L, 41L, 41L, 41L, 41L, 43L, 66L, 66L, 66L, 66L, 66L, 67L, 
    67L, 67L, 67L, 67L, 67L, 67L, 68L, 68L, 68L, 71L, 71L, 71L, 
    72L, 72L, 72L, 72L, 73L, 73L, 73L, 73L, 75L, 75L, 76L, 76L, 
    76L, 78L, 78L, 80L, 80L, 85L, 85L, 85L, 86L, 86L, 86L, 86L, 
    86L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 90L, 90L, 90L, 
    90L, 93L, 93L, 93L, 94L, 94L, 94L, 95L, 95L, 103L, 103L, 
    103L, 103L, 103L, 106L, 107L, 111L, 111L, 121L, 121L, 134L, 
    134L, 134L, 134L, 134L, 135L, 135L, 136L, 137L, 138L), .Label = c(" Chain1  ", 
    " Chain10 ", " Chain100", " Chain101", " Chain102", " Chain103", 
    " Chain104", " Chain105", " Chain106", " Chain107", " Chain108", 
    " Chain109", " Chain11 ", " Chain110", " Chain112", " Chain113", 
    " Chain114", " Chain115", " Chain116", " Chain117", " Chain118", 
    " Chain119", " Chain120", " Chain122", " Chain124", " Chain125", 
    " Chain126", " Chain127", " Chain128", " Chain129", " Chain13 ", 
    " Chain130", " Chain131", " Chain132", " Chain133", " Chain134", 
    " Chain135", " Chain136", " Chain137", " Chain14 ", " Chain15 ", 
    " Chain16 ", " Chain17 ", " Chain18 ", " Chain20 ", " Chain22 ", 
    " Chain23 ", " Chain24 ", " Chain25 ", " Chain27 ", " Chain28 ", 
    " Chain29 ", " Chain3  ", " Chain30 ", " Chain31 ", " Chain32 ", 
    " Chain33 ", " Chain34 ", " Chain35 ", " Chain36 ", " Chain37 ", 
    " Chain39 ", " Chain4  ", " Chain40 ", " Chain41 ", " Chain42 ", 
    " Chain43 ", " Chain44 ", " Chain45 ", " Chain46 ", " Chain47 ", 
    " Chain48 ", " Chain49 ", " Chain5  ", " Chain50 ", " Chain52 ", 
    " Chain53 ", " Chain54 ", " Chain55 ", " Chain57 ", " Chain58 ", 
    " Chain59 ", " Chain6  ", " Chain60 ", " Chain61 ", " Chain62 ", 
    " Chain63 ", " Chain64 ", " Chain65 ", " Chain66 ", " Chain69 ", 
    " Chain7  ", " Chain70 ", " Chain71 ", " Chain72 ", " Chain73 ", 
    " Chain74 ", " Chain75 ", " Chain76 ", " Chain77 ", " Chain78 ", 
    " Chain79 ", " Chain8  ", " Chain80 ", " Chain81 ", " Chain83 ", 
    " Chain84 ", " Chain85 ", " Chain86 ", " Chain87 ", " Chain88 ", 
    " Chain89 ", " Chain9  ", " Chain90 ", " Chain91 ", " Chain92 ", 
    " Chain94 ", " Chain95 ", " Chain97 ", " Chain98 ", " Chain99 ", 
    " NONE    ", " Chain111", " Chain12 ", " Chain121", " Chain123", 
    " Chain19 ", " Chain2  ", " Chain21 ", " Chain26 ", " Chain38 ", 
    " Chain51 ", " Chain56 ", " Chain67 ", " Chain68 ", " Chain82 ", 
    " Chain96 ", " Chain93 "), class = "factor"), month = c(2, 
    3, 12, 5, 8, 5, 6, 9, 1, 1, 8, 10, 11, 11, 3, 4, 5, 7, 11, 
    1, 2, 3, 7, 7, 8, 12, 6, 8, 10, 1, 9, 12, 9, 10, 12, 12, 
    2, 5, 10, 10, 4, 11, 1, 3, 6, 10, 12, 3, 11, 5, 9, 10, 1, 
    3, 6, 9, 12, 1, 2, 4, 9, 12, 1, 3, 4, 8, 9, 9, 12, 5, 8, 
    11, 3, 9, 11, 11, 12, 2, 5, 7, 9, 11, 5, 8, 8, 11, 6, 12, 
    2, 4, 6, 12, 12, 8, 12, 5, 5, 8), year = c(2003, 2002, 2001, 
    2003, 2001, 2001, 2001, 2001, 2011, 2012, 2011, 2011, 2012, 
    2009, 2008, 2008, 2008, 2008, 2008, 2011, 2011, 2012, 2011, 
    2012, 2011, 2012, 2010, 2010, 2010, 2009, 2009, 2009, 2006, 
    2007, 2005, 2006, 2007, 2005, 2005, 2007, 2004, 2004, 2003, 
    2003, 2003, 2002, 2001, 2001, 2001, 2011, 2012, 2011, 2010, 
    2010, 2010, 2010, 2010, 2007, 2009, 2007, 2007, 2007, 2011, 
    2008, 2005, 2008, 2005, 2011, 2010, 2003, 2003, 2003, 2009, 
    2009, 2009, 2002, 2002, 2005, 2003, 2004, 2003, 2003, 2012, 
    2002, 2009, 2009, 2001, 2007, 2012, 2004, 2012, 2004, 2012, 
    2010, 2010, 2003, 2002, 2003)), class = "data.frame", row.names = c(NA, 
-98L), vars = c("MskdName", "month", "year"), drop = TRUE))

1 个答案:

答案 0 :(得分:-1)

#Convert to character
df1$MskdName = as.character(df1$MskdName)
df1$L5 = as.character(df1$L5)

myChains = unique(df1$MskdName)
productsChains = split(df1$MskdName, df1$L5)
df2 = sapply(productsChains, function(x) as.numeric(myChains %in% x))
row.names(df2) = myChains