How to replicate all rows of a dataframe for each ID of another dataframe in R? The Next CEO of Stack OverflowHow to sort a dataframe by multiple column(s)?Remove rows with all or some NAs (missing values) in data.frameHow do I replace NA values with zeros in an R dataframe?How to delete rows from a dataframe that contain n*NAhow to take mean of a subset of a dataframe within for loop in rHow to create a new dataframe with summarized data in R?Consolidating duplicate rows in a dataframeHow to generate, populate and update a new dataframe based on an old dataframe?Reshaping a dataframe by moving part of an existing row to a new rowUse dplyr to change an R dataframe from second row across multiple columns

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Interfacing a button to MCU (and PC) with 50m long cable

What does "Its cash flow is deeply negative" mean?

Is there a way to save my career from absolute disaster?

WOW air has ceased operation, can I get my tickets refunded?

Why do professional authors make "consistency" mistakes? And how to avoid them?

sp_blitzCache results Memory grants

How to add tiny 0.5A 120V load to very remote split phase 240v 3 wire well house

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Return the Closest Prime Number

How to avoid supervisors with prejudiced views?

Is there a difference between "Fahrstuhl" and "Aufzug"

If a black hole is created from light, can this black hole then move at speed of light?

Why do we use the plural of movies in this phrase "We went to the movies last night."?

Skipping indices in a product

What exact does MIB represent in SNMP? How is it different from OID?

Anatomically Correct Strange Women In Ponds Distributing Swords

Does it take more energy to get to Venus or to Mars?

How do scammers retract money, while you can’t?

Can I equip Skullclamp on a creature I am sacrificing?

How do I go from 300 unfinished/half written blog posts, to published posts?

How do we know the LHC results are robust?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Won the lottery - how do I keep the money?



How to replicate all rows of a dataframe for each ID of another dataframe in R?



The Next CEO of Stack OverflowHow to sort a dataframe by multiple column(s)?Remove rows with all or some NAs (missing values) in data.frameHow do I replace NA values with zeros in an R dataframe?How to delete rows from a dataframe that contain n*NAhow to take mean of a subset of a dataframe within for loop in rHow to create a new dataframe with summarized data in R?Consolidating duplicate rows in a dataframeHow to generate, populate and update a new dataframe based on an old dataframe?Reshaping a dataframe by moving part of an existing row to a new rowUse dplyr to change an R dataframe from second row across multiple columns










0















I have one dataframe (df_features) consisting of 32 rows and six columns that relate to potential features of a study and a second dataframe (df_participants) containing 10,000 unique (non-numeric) IDs of my participants. There are no common columns across the two dataframes.



I want to create a dataset that contains each of the 32 rows from df_features for every ID in df_participants (so 320,000 rows and 7 columns in total).



How do I do this? I feel like it should be straightforward but I just can't find anything anywhere!










share|improve this question


























    0















    I have one dataframe (df_features) consisting of 32 rows and six columns that relate to potential features of a study and a second dataframe (df_participants) containing 10,000 unique (non-numeric) IDs of my participants. There are no common columns across the two dataframes.



    I want to create a dataset that contains each of the 32 rows from df_features for every ID in df_participants (so 320,000 rows and 7 columns in total).



    How do I do this? I feel like it should be straightforward but I just can't find anything anywhere!










    share|improve this question
























      0












      0








      0








      I have one dataframe (df_features) consisting of 32 rows and six columns that relate to potential features of a study and a second dataframe (df_participants) containing 10,000 unique (non-numeric) IDs of my participants. There are no common columns across the two dataframes.



      I want to create a dataset that contains each of the 32 rows from df_features for every ID in df_participants (so 320,000 rows and 7 columns in total).



      How do I do this? I feel like it should be straightforward but I just can't find anything anywhere!










      share|improve this question














      I have one dataframe (df_features) consisting of 32 rows and six columns that relate to potential features of a study and a second dataframe (df_participants) containing 10,000 unique (non-numeric) IDs of my participants. There are no common columns across the two dataframes.



      I want to create a dataset that contains each of the 32 rows from df_features for every ID in df_participants (so 320,000 rows and 7 columns in total).



      How do I do this? I feel like it should be straightforward but I just can't find anything anywhere!







      r join






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 21 at 17:03









      MelMel

      396




      396






















          3 Answers
          3






          active

          oldest

          votes


















          1














          It sounds like you are looking to do a full outer join that will combine all features with all IDs. This can be done using several packages, and in base-R with the following:



          features <- data.frame(f1=c("blue","geeen"),f2=c("young","old"))
          participants <- data.frame(ID=c(1:10))

          merge(features,participants,all=T)





          share|improve this answer























          • Thanks, that's much better than the ridiculous method I initially used!

            – Mel
            Mar 21 at 17:30


















          0














          You can do a full outer join. When you do a full outer join with no common columns across the two dataframes, you get the cartesian product of the two dataframes, which is what you're looking for. You can get this using the merge function. If your only two arguments to merge are the dataframes you want to perform the join on, you will get back the cartesian product of those dataframes.



          Example:



          df1 <- data.frame(y = 1:4)
          df2 <- data.frame(z = 1:3)
          df_merged <- merge(df1, df2)
          print(df1)
          # y
          #1 1
          #2 2
          #3 3
          #4 4

          print(df2)
          # z
          #1 1
          #2 2
          #3 3

          print(df_merged)
          # y z
          #1 1 1
          #2 2 1
          #3 3 1
          #4 4 1
          #5 1 2
          #6 2 2
          #7 3 2
          #8 4 2
          #9 1 3
          #10 2 3
          #11 3 3
          #12 4 3





          share|improve this answer
































            0














            I found a fairly convoluted way around it in case anyone is looking to do something similar:



            matching_1<- expand.grid(df_participants$ID, df_features$feature_rownumber) %>% arrange(Var1) %>%
            rename("ID"=Var1, "feature_rownumber"=Var2)

            matching_2 <- left_join(df_participants, matching_1, by="ID")

            final_dataset <- left_join(matching_2, df_features, by="feature_rownumber")


            However I'm fairly sure there must be a more concise method out there!






            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%2f55285670%2fhow-to-replicate-all-rows-of-a-dataframe-for-each-id-of-another-dataframe-in-r%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              It sounds like you are looking to do a full outer join that will combine all features with all IDs. This can be done using several packages, and in base-R with the following:



              features <- data.frame(f1=c("blue","geeen"),f2=c("young","old"))
              participants <- data.frame(ID=c(1:10))

              merge(features,participants,all=T)





              share|improve this answer























              • Thanks, that's much better than the ridiculous method I initially used!

                – Mel
                Mar 21 at 17:30















              1














              It sounds like you are looking to do a full outer join that will combine all features with all IDs. This can be done using several packages, and in base-R with the following:



              features <- data.frame(f1=c("blue","geeen"),f2=c("young","old"))
              participants <- data.frame(ID=c(1:10))

              merge(features,participants,all=T)





              share|improve this answer























              • Thanks, that's much better than the ridiculous method I initially used!

                – Mel
                Mar 21 at 17:30













              1












              1








              1







              It sounds like you are looking to do a full outer join that will combine all features with all IDs. This can be done using several packages, and in base-R with the following:



              features <- data.frame(f1=c("blue","geeen"),f2=c("young","old"))
              participants <- data.frame(ID=c(1:10))

              merge(features,participants,all=T)





              share|improve this answer













              It sounds like you are looking to do a full outer join that will combine all features with all IDs. This can be done using several packages, and in base-R with the following:



              features <- data.frame(f1=c("blue","geeen"),f2=c("young","old"))
              participants <- data.frame(ID=c(1:10))

              merge(features,participants,all=T)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 21 at 17:17









              SorenSoren

              1,2431711




              1,2431711












              • Thanks, that's much better than the ridiculous method I initially used!

                – Mel
                Mar 21 at 17:30

















              • Thanks, that's much better than the ridiculous method I initially used!

                – Mel
                Mar 21 at 17:30
















              Thanks, that's much better than the ridiculous method I initially used!

              – Mel
              Mar 21 at 17:30





              Thanks, that's much better than the ridiculous method I initially used!

              – Mel
              Mar 21 at 17:30













              0














              You can do a full outer join. When you do a full outer join with no common columns across the two dataframes, you get the cartesian product of the two dataframes, which is what you're looking for. You can get this using the merge function. If your only two arguments to merge are the dataframes you want to perform the join on, you will get back the cartesian product of those dataframes.



              Example:



              df1 <- data.frame(y = 1:4)
              df2 <- data.frame(z = 1:3)
              df_merged <- merge(df1, df2)
              print(df1)
              # y
              #1 1
              #2 2
              #3 3
              #4 4

              print(df2)
              # z
              #1 1
              #2 2
              #3 3

              print(df_merged)
              # y z
              #1 1 1
              #2 2 1
              #3 3 1
              #4 4 1
              #5 1 2
              #6 2 2
              #7 3 2
              #8 4 2
              #9 1 3
              #10 2 3
              #11 3 3
              #12 4 3





              share|improve this answer





























                0














                You can do a full outer join. When you do a full outer join with no common columns across the two dataframes, you get the cartesian product of the two dataframes, which is what you're looking for. You can get this using the merge function. If your only two arguments to merge are the dataframes you want to perform the join on, you will get back the cartesian product of those dataframes.



                Example:



                df1 <- data.frame(y = 1:4)
                df2 <- data.frame(z = 1:3)
                df_merged <- merge(df1, df2)
                print(df1)
                # y
                #1 1
                #2 2
                #3 3
                #4 4

                print(df2)
                # z
                #1 1
                #2 2
                #3 3

                print(df_merged)
                # y z
                #1 1 1
                #2 2 1
                #3 3 1
                #4 4 1
                #5 1 2
                #6 2 2
                #7 3 2
                #8 4 2
                #9 1 3
                #10 2 3
                #11 3 3
                #12 4 3





                share|improve this answer



























                  0












                  0








                  0







                  You can do a full outer join. When you do a full outer join with no common columns across the two dataframes, you get the cartesian product of the two dataframes, which is what you're looking for. You can get this using the merge function. If your only two arguments to merge are the dataframes you want to perform the join on, you will get back the cartesian product of those dataframes.



                  Example:



                  df1 <- data.frame(y = 1:4)
                  df2 <- data.frame(z = 1:3)
                  df_merged <- merge(df1, df2)
                  print(df1)
                  # y
                  #1 1
                  #2 2
                  #3 3
                  #4 4

                  print(df2)
                  # z
                  #1 1
                  #2 2
                  #3 3

                  print(df_merged)
                  # y z
                  #1 1 1
                  #2 2 1
                  #3 3 1
                  #4 4 1
                  #5 1 2
                  #6 2 2
                  #7 3 2
                  #8 4 2
                  #9 1 3
                  #10 2 3
                  #11 3 3
                  #12 4 3





                  share|improve this answer















                  You can do a full outer join. When you do a full outer join with no common columns across the two dataframes, you get the cartesian product of the two dataframes, which is what you're looking for. You can get this using the merge function. If your only two arguments to merge are the dataframes you want to perform the join on, you will get back the cartesian product of those dataframes.



                  Example:



                  df1 <- data.frame(y = 1:4)
                  df2 <- data.frame(z = 1:3)
                  df_merged <- merge(df1, df2)
                  print(df1)
                  # y
                  #1 1
                  #2 2
                  #3 3
                  #4 4

                  print(df2)
                  # z
                  #1 1
                  #2 2
                  #3 3

                  print(df_merged)
                  # y z
                  #1 1 1
                  #2 2 1
                  #3 3 1
                  #4 4 1
                  #5 1 2
                  #6 2 2
                  #7 3 2
                  #8 4 2
                  #9 1 3
                  #10 2 3
                  #11 3 3
                  #12 4 3






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 21 at 17:20

























                  answered Mar 21 at 17:14









                  David RosenmanDavid Rosenman

                  63239




                  63239





















                      0














                      I found a fairly convoluted way around it in case anyone is looking to do something similar:



                      matching_1<- expand.grid(df_participants$ID, df_features$feature_rownumber) %>% arrange(Var1) %>%
                      rename("ID"=Var1, "feature_rownumber"=Var2)

                      matching_2 <- left_join(df_participants, matching_1, by="ID")

                      final_dataset <- left_join(matching_2, df_features, by="feature_rownumber")


                      However I'm fairly sure there must be a more concise method out there!






                      share|improve this answer



























                        0














                        I found a fairly convoluted way around it in case anyone is looking to do something similar:



                        matching_1<- expand.grid(df_participants$ID, df_features$feature_rownumber) %>% arrange(Var1) %>%
                        rename("ID"=Var1, "feature_rownumber"=Var2)

                        matching_2 <- left_join(df_participants, matching_1, by="ID")

                        final_dataset <- left_join(matching_2, df_features, by="feature_rownumber")


                        However I'm fairly sure there must be a more concise method out there!






                        share|improve this answer

























                          0












                          0








                          0







                          I found a fairly convoluted way around it in case anyone is looking to do something similar:



                          matching_1<- expand.grid(df_participants$ID, df_features$feature_rownumber) %>% arrange(Var1) %>%
                          rename("ID"=Var1, "feature_rownumber"=Var2)

                          matching_2 <- left_join(df_participants, matching_1, by="ID")

                          final_dataset <- left_join(matching_2, df_features, by="feature_rownumber")


                          However I'm fairly sure there must be a more concise method out there!






                          share|improve this answer













                          I found a fairly convoluted way around it in case anyone is looking to do something similar:



                          matching_1<- expand.grid(df_participants$ID, df_features$feature_rownumber) %>% arrange(Var1) %>%
                          rename("ID"=Var1, "feature_rownumber"=Var2)

                          matching_2 <- left_join(df_participants, matching_1, by="ID")

                          final_dataset <- left_join(matching_2, df_features, by="feature_rownumber")


                          However I'm fairly sure there must be a more concise method out there!







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 21 at 17:27









                          MelMel

                          396




                          396



























                              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%2f55285670%2fhow-to-replicate-all-rows-of-a-dataframe-for-each-id-of-another-dataframe-in-r%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