Is there a way to create a data frame with rows for combinations of variables in other data frames?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameCreate an empty data.frameMerge data frames of unequal number of rowsCreate vector of data frame subsets based on group by of columnsChanging Column Names in a List of Data Frames in RHow to substitute some values of one dataframe with other data frame in R?How to keep merging data frames using a function in R?Python Pandas - Find difference between two data framesPandas create a data frame based on two other 'sub' frames

What is the name of this OOB notification method/popup, and is it customizable?

Are there any features that help with the roll to avoid the destruction of a Wand of Fireballs when using the last charge?

Does anyone know what these symbols mean?

Averting Real Women Don’t Wear Dresses

Most elegant way to write a one shot IF

Why do I need two parameters in an HTTP parameter pollution attack?

What is "oversubscription" in Networking?

What could a reptilian race tell by candling their eggs?

One folder two different locations on ubuntu 18.04

Needle Hotend for nonplanar printing

Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?

Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?

Mean Value Theorem: Continuous or Defined?

Details of video memory access arbitration in Space Invaders

The Confused Alien

Who are these Discworld wizards from this picture?

How exactly is a normal force exerted, at the molecular level?

How do I tell the reader that my character is autistic in Fantasy?

Does Anosov geodesic flow imply asphericity?

Why did this meteor appear cyan?

Most importants new papers in computational complexity

Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?

How to fix a dry solder pin in BGA package?

Why was Mal so quick to drop Bester in favour of Kaylee?



Is there a way to create a data frame with rows for combinations of variables in other data frames?


How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameCreate an empty data.frameMerge data frames of unequal number of rowsCreate vector of data frame subsets based on group by of columnsChanging Column Names in a List of Data Frames in RHow to substitute some values of one dataframe with other data frame in R?How to keep merging data frames using a function in R?Python Pandas - Find difference between two data framesPandas create a data frame based on two other 'sub' frames






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.



I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.



# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")

# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")









share|improve this question






















  • For completeness: data.table::CJ

    – r2evans
    Mar 25 at 12:48

















0















I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.



I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.



# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")

# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")









share|improve this question






















  • For completeness: data.table::CJ

    – r2evans
    Mar 25 at 12:48













0












0








0








I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.



I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.



# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")

# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")









share|improve this question














I have several data frames containing one column each. I want to create a data frame which has rows for each possible combination of the values in the other data frames.



I tried a workaround with nested for loops for each data frame but since i have about 20 data frames it became tedious quite fast.



# the data frames I already have
df1 <- data.frame(c("A","B","C"))
colnames(df1) <- c("x")
df2 <- data.frame(c("ALPHA","BRAVO","CHARLIE"))
colnames(df2) <- c("y")

# the data frame I want to create
df3 <- data.frame(c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
c("ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE", "ALPHA","BRAVO","CHARLIE"))
colnames(df3) <- c("x", "y")






r dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 12:35









erikfjonssonerikfjonsson

487 bronze badges




487 bronze badges












  • For completeness: data.table::CJ

    – r2evans
    Mar 25 at 12:48

















  • For completeness: data.table::CJ

    – r2evans
    Mar 25 at 12:48
















For completeness: data.table::CJ

– r2evans
Mar 25 at 12:48





For completeness: data.table::CJ

– r2evans
Mar 25 at 12:48












2 Answers
2






active

oldest

votes


















3














One option is to use tidyr::crossing



tidyr::crossing(df1, df2)

# df1$x df2$y
# <fct> <fct>
#1 A ALPHA
#2 A BRAVO
#3 A CHARLIE
#4 B ALPHA
#5 B BRAVO
#6 B CHARLIE
#7 C ALPHA
#8 C BRAVO
#9 C CHARLIE


You can just add all the dataframes in it and it would give you output for all combinations of it.




In base R, you could also use merge in Reduce by putting all the dataframes in one list.



Reduce(merge, list(df1, df2))





share|improve this answer
































    0














    We can use CJ from data.table



    library(data.table)
    CJ(x= df1$x, y = df2$y)
    # x y
    #1: A ALPHA
    #2: A BRAVO
    #3: A CHARLIE
    #4: B ALPHA
    #5: B BRAVO
    #6: B CHARLIE
    #7: C ALPHA
    #8: C BRAVO
    #9: C CHARLIE





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55337933%2fis-there-a-way-to-create-a-data-frame-with-rows-for-combinations-of-variables-in%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      One option is to use tidyr::crossing



      tidyr::crossing(df1, df2)

      # df1$x df2$y
      # <fct> <fct>
      #1 A ALPHA
      #2 A BRAVO
      #3 A CHARLIE
      #4 B ALPHA
      #5 B BRAVO
      #6 B CHARLIE
      #7 C ALPHA
      #8 C BRAVO
      #9 C CHARLIE


      You can just add all the dataframes in it and it would give you output for all combinations of it.




      In base R, you could also use merge in Reduce by putting all the dataframes in one list.



      Reduce(merge, list(df1, df2))





      share|improve this answer





























        3














        One option is to use tidyr::crossing



        tidyr::crossing(df1, df2)

        # df1$x df2$y
        # <fct> <fct>
        #1 A ALPHA
        #2 A BRAVO
        #3 A CHARLIE
        #4 B ALPHA
        #5 B BRAVO
        #6 B CHARLIE
        #7 C ALPHA
        #8 C BRAVO
        #9 C CHARLIE


        You can just add all the dataframes in it and it would give you output for all combinations of it.




        In base R, you could also use merge in Reduce by putting all the dataframes in one list.



        Reduce(merge, list(df1, df2))





        share|improve this answer



























          3












          3








          3







          One option is to use tidyr::crossing



          tidyr::crossing(df1, df2)

          # df1$x df2$y
          # <fct> <fct>
          #1 A ALPHA
          #2 A BRAVO
          #3 A CHARLIE
          #4 B ALPHA
          #5 B BRAVO
          #6 B CHARLIE
          #7 C ALPHA
          #8 C BRAVO
          #9 C CHARLIE


          You can just add all the dataframes in it and it would give you output for all combinations of it.




          In base R, you could also use merge in Reduce by putting all the dataframes in one list.



          Reduce(merge, list(df1, df2))





          share|improve this answer















          One option is to use tidyr::crossing



          tidyr::crossing(df1, df2)

          # df1$x df2$y
          # <fct> <fct>
          #1 A ALPHA
          #2 A BRAVO
          #3 A CHARLIE
          #4 B ALPHA
          #5 B BRAVO
          #6 B CHARLIE
          #7 C ALPHA
          #8 C BRAVO
          #9 C CHARLIE


          You can just add all the dataframes in it and it would give you output for all combinations of it.




          In base R, you could also use merge in Reduce by putting all the dataframes in one list.



          Reduce(merge, list(df1, df2))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 12:48

























          answered Mar 25 at 12:42









          Ronak ShahRonak Shah

          62.7k10 gold badges46 silver badges80 bronze badges




          62.7k10 gold badges46 silver badges80 bronze badges























              0














              We can use CJ from data.table



              library(data.table)
              CJ(x= df1$x, y = df2$y)
              # x y
              #1: A ALPHA
              #2: A BRAVO
              #3: A CHARLIE
              #4: B ALPHA
              #5: B BRAVO
              #6: B CHARLIE
              #7: C ALPHA
              #8: C BRAVO
              #9: C CHARLIE





              share|improve this answer



























                0














                We can use CJ from data.table



                library(data.table)
                CJ(x= df1$x, y = df2$y)
                # x y
                #1: A ALPHA
                #2: A BRAVO
                #3: A CHARLIE
                #4: B ALPHA
                #5: B BRAVO
                #6: B CHARLIE
                #7: C ALPHA
                #8: C BRAVO
                #9: C CHARLIE





                share|improve this answer

























                  0












                  0








                  0







                  We can use CJ from data.table



                  library(data.table)
                  CJ(x= df1$x, y = df2$y)
                  # x y
                  #1: A ALPHA
                  #2: A BRAVO
                  #3: A CHARLIE
                  #4: B ALPHA
                  #5: B BRAVO
                  #6: B CHARLIE
                  #7: C ALPHA
                  #8: C BRAVO
                  #9: C CHARLIE





                  share|improve this answer













                  We can use CJ from data.table



                  library(data.table)
                  CJ(x= df1$x, y = df2$y)
                  # x y
                  #1: A ALPHA
                  #2: A BRAVO
                  #3: A CHARLIE
                  #4: B ALPHA
                  #5: B BRAVO
                  #6: B CHARLIE
                  #7: C ALPHA
                  #8: C BRAVO
                  #9: C CHARLIE






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 15:08









                  akrunakrun

                  443k14 gold badges245 silver badges325 bronze badges




                  443k14 gold badges245 silver badges325 bronze badges



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55337933%2fis-there-a-way-to-create-a-data-frame-with-rows-for-combinations-of-variables-in%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript