Merging multiple data frames by pasting alternative rows












0















I have spatial weather data of UK in Ascii text format for 7 years (each file/dataframe have monthly data of one year - 12 columns and 52201 rows as each row represents one location).
I want to merge data frames with alternative rows - 1st row of data frame 1 then 1st row of data frame 2 till 1st row of data frame 7 then 2nd row of data frame 1 till 2nd row of data frame 7 and continue ....



  V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12
-1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199
-1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299
-1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399
-9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999
-9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999


Here is my code:



n1<-read.table("data/y1call.txt", sep="")
n2<-read.table("data/y2call.txt", sep="")
n3<-read.table("data/y3call.txt", sep="")
c<-rbind(n1,n2,n3)
merge(n1,n2,n3)


I have tried merge, rbind, cbind but all failed.










share|improve this question





























    0















    I have spatial weather data of UK in Ascii text format for 7 years (each file/dataframe have monthly data of one year - 12 columns and 52201 rows as each row represents one location).
    I want to merge data frames with alternative rows - 1st row of data frame 1 then 1st row of data frame 2 till 1st row of data frame 7 then 2nd row of data frame 1 till 2nd row of data frame 7 and continue ....



      V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12
    -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199
    -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299
    -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399
    -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999
    -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999


    Here is my code:



    n1<-read.table("data/y1call.txt", sep="")
    n2<-read.table("data/y2call.txt", sep="")
    n3<-read.table("data/y3call.txt", sep="")
    c<-rbind(n1,n2,n3)
    merge(n1,n2,n3)


    I have tried merge, rbind, cbind but all failed.










    share|improve this question



























      0












      0








      0








      I have spatial weather data of UK in Ascii text format for 7 years (each file/dataframe have monthly data of one year - 12 columns and 52201 rows as each row represents one location).
      I want to merge data frames with alternative rows - 1st row of data frame 1 then 1st row of data frame 2 till 1st row of data frame 7 then 2nd row of data frame 1 till 2nd row of data frame 7 and continue ....



        V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12
      -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199
      -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299
      -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399
      -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999
      -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999


      Here is my code:



      n1<-read.table("data/y1call.txt", sep="")
      n2<-read.table("data/y2call.txt", sep="")
      n3<-read.table("data/y3call.txt", sep="")
      c<-rbind(n1,n2,n3)
      merge(n1,n2,n3)


      I have tried merge, rbind, cbind but all failed.










      share|improve this question
















      I have spatial weather data of UK in Ascii text format for 7 years (each file/dataframe have monthly data of one year - 12 columns and 52201 rows as each row represents one location).
      I want to merge data frames with alternative rows - 1st row of data frame 1 then 1st row of data frame 2 till 1st row of data frame 7 then 2nd row of data frame 1 till 2nd row of data frame 7 and continue ....



        V1    V2    V3    V4    V5    V6    V7    V8    V9   V10   V11   V12
      -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199 -1199
      -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299 -1299
      -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399 -1399
      -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999
      -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999


      Here is my code:



      n1<-read.table("data/y1call.txt", sep="")
      n2<-read.table("data/y2call.txt", sep="")
      n3<-read.table("data/y3call.txt", sep="")
      c<-rbind(n1,n2,n3)
      merge(n1,n2,n3)


      I have tried merge, rbind, cbind but all failed.







      r dataframe merge rbind cbind






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 13:02









      Sotos

      30.7k51641




      30.7k51641










      asked Nov 14 '18 at 12:43









      NaveedNaveed

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can create a new column representing the rownames in each data frame, and sort on that variable after you rbind, i.e.



          ddf_all <- do.call(rbind, lapply(list(df1, df2, df3), function(i)transform(i, new = rownames(i))))
          ddf_all[order(ddf_all$new),]


          which gives,




             v1 v2 new
          1 1 6 1
          6 11 16 1
          11 21 26 1
          2 2 7 2
          7 12 17 2
          12 22 27 2
          3 3 8 3
          8 13 18 3
          13 23 28 3
          4 4 9 4
          9 14 19 4
          14 24 29 4
          5 5 10 5
          10 15 20 5
          15 25 30 5



          DATA:



          df1 <- data.frame(v1 = c(1, 2, 3, 4, 5), V2 = c(6, 7, 8, 9, 10))
          df2 <- data.frame(v1 = c(11, 12, 13, 14, 15), v2 = c(16, 17, 18, 19, 20))
          df3 <- data.frame(v1 = c(21, 22, 23, 24, 25), v2 = c(26, 27, 28, 29, 30))





          share|improve this answer
























          • Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

            – Naveed
            Nov 15 '18 at 14:42











          • I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

            – Sotos
            Nov 15 '18 at 14:51











          • n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

            – Naveed
            Nov 15 '18 at 15:17













          • Well, that does not help me. I do not have the files data/y1call.txt, ... :)

            – Sotos
            Nov 15 '18 at 15:19











          • Can I get your email to send files and the code? Thanks

            – Naveed
            Nov 15 '18 at 15:23











          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%2f53300519%2fmerging-multiple-data-frames-by-pasting-alternative-rows%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can create a new column representing the rownames in each data frame, and sort on that variable after you rbind, i.e.



          ddf_all <- do.call(rbind, lapply(list(df1, df2, df3), function(i)transform(i, new = rownames(i))))
          ddf_all[order(ddf_all$new),]


          which gives,




             v1 v2 new
          1 1 6 1
          6 11 16 1
          11 21 26 1
          2 2 7 2
          7 12 17 2
          12 22 27 2
          3 3 8 3
          8 13 18 3
          13 23 28 3
          4 4 9 4
          9 14 19 4
          14 24 29 4
          5 5 10 5
          10 15 20 5
          15 25 30 5



          DATA:



          df1 <- data.frame(v1 = c(1, 2, 3, 4, 5), V2 = c(6, 7, 8, 9, 10))
          df2 <- data.frame(v1 = c(11, 12, 13, 14, 15), v2 = c(16, 17, 18, 19, 20))
          df3 <- data.frame(v1 = c(21, 22, 23, 24, 25), v2 = c(26, 27, 28, 29, 30))





          share|improve this answer
























          • Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

            – Naveed
            Nov 15 '18 at 14:42











          • I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

            – Sotos
            Nov 15 '18 at 14:51











          • n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

            – Naveed
            Nov 15 '18 at 15:17













          • Well, that does not help me. I do not have the files data/y1call.txt, ... :)

            – Sotos
            Nov 15 '18 at 15:19











          • Can I get your email to send files and the code? Thanks

            – Naveed
            Nov 15 '18 at 15:23
















          0














          You can create a new column representing the rownames in each data frame, and sort on that variable after you rbind, i.e.



          ddf_all <- do.call(rbind, lapply(list(df1, df2, df3), function(i)transform(i, new = rownames(i))))
          ddf_all[order(ddf_all$new),]


          which gives,




             v1 v2 new
          1 1 6 1
          6 11 16 1
          11 21 26 1
          2 2 7 2
          7 12 17 2
          12 22 27 2
          3 3 8 3
          8 13 18 3
          13 23 28 3
          4 4 9 4
          9 14 19 4
          14 24 29 4
          5 5 10 5
          10 15 20 5
          15 25 30 5



          DATA:



          df1 <- data.frame(v1 = c(1, 2, 3, 4, 5), V2 = c(6, 7, 8, 9, 10))
          df2 <- data.frame(v1 = c(11, 12, 13, 14, 15), v2 = c(16, 17, 18, 19, 20))
          df3 <- data.frame(v1 = c(21, 22, 23, 24, 25), v2 = c(26, 27, 28, 29, 30))





          share|improve this answer
























          • Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

            – Naveed
            Nov 15 '18 at 14:42











          • I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

            – Sotos
            Nov 15 '18 at 14:51











          • n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

            – Naveed
            Nov 15 '18 at 15:17













          • Well, that does not help me. I do not have the files data/y1call.txt, ... :)

            – Sotos
            Nov 15 '18 at 15:19











          • Can I get your email to send files and the code? Thanks

            – Naveed
            Nov 15 '18 at 15:23














          0












          0








          0







          You can create a new column representing the rownames in each data frame, and sort on that variable after you rbind, i.e.



          ddf_all <- do.call(rbind, lapply(list(df1, df2, df3), function(i)transform(i, new = rownames(i))))
          ddf_all[order(ddf_all$new),]


          which gives,




             v1 v2 new
          1 1 6 1
          6 11 16 1
          11 21 26 1
          2 2 7 2
          7 12 17 2
          12 22 27 2
          3 3 8 3
          8 13 18 3
          13 23 28 3
          4 4 9 4
          9 14 19 4
          14 24 29 4
          5 5 10 5
          10 15 20 5
          15 25 30 5



          DATA:



          df1 <- data.frame(v1 = c(1, 2, 3, 4, 5), V2 = c(6, 7, 8, 9, 10))
          df2 <- data.frame(v1 = c(11, 12, 13, 14, 15), v2 = c(16, 17, 18, 19, 20))
          df3 <- data.frame(v1 = c(21, 22, 23, 24, 25), v2 = c(26, 27, 28, 29, 30))





          share|improve this answer













          You can create a new column representing the rownames in each data frame, and sort on that variable after you rbind, i.e.



          ddf_all <- do.call(rbind, lapply(list(df1, df2, df3), function(i)transform(i, new = rownames(i))))
          ddf_all[order(ddf_all$new),]


          which gives,




             v1 v2 new
          1 1 6 1
          6 11 16 1
          11 21 26 1
          2 2 7 2
          7 12 17 2
          12 22 27 2
          3 3 8 3
          8 13 18 3
          13 23 28 3
          4 4 9 4
          9 14 19 4
          14 24 29 4
          5 5 10 5
          10 15 20 5
          15 25 30 5



          DATA:



          df1 <- data.frame(v1 = c(1, 2, 3, 4, 5), V2 = c(6, 7, 8, 9, 10))
          df2 <- data.frame(v1 = c(11, 12, 13, 14, 15), v2 = c(16, 17, 18, 19, 20))
          df3 <- data.frame(v1 = c(21, 22, 23, 24, 25), v2 = c(26, 27, 28, 29, 30))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:01









          SotosSotos

          30.7k51641




          30.7k51641













          • Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

            – Naveed
            Nov 15 '18 at 14:42











          • I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

            – Sotos
            Nov 15 '18 at 14:51











          • n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

            – Naveed
            Nov 15 '18 at 15:17













          • Well, that does not help me. I do not have the files data/y1call.txt, ... :)

            – Sotos
            Nov 15 '18 at 15:19











          • Can I get your email to send files and the code? Thanks

            – Naveed
            Nov 15 '18 at 15:23



















          • Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

            – Naveed
            Nov 15 '18 at 14:42











          • I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

            – Sotos
            Nov 15 '18 at 14:51











          • n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

            – Naveed
            Nov 15 '18 at 15:17













          • Well, that does not help me. I do not have the files data/y1call.txt, ... :)

            – Sotos
            Nov 15 '18 at 15:19











          • Can I get your email to send files and the code? Thanks

            – Naveed
            Nov 15 '18 at 15:23

















          Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

          – Naveed
          Nov 15 '18 at 14:42





          Dear Sotos, Thanks for your input and its working well but it's still there is some unexpected change. As the row sequence/order should be 1,6,11 and then 2,7,12 and so on but here in my case it is 1,6,11 but no 2,7,12 and something different order. Do you know why this can happen or any other solution?

          – Naveed
          Nov 15 '18 at 14:42













          I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

          – Sotos
          Nov 15 '18 at 14:51





          I wouldn't know until I see an example that fails. I don't see anything flawed in the workflow. Are the individual data frames ordered? Do you need to order them individually before throwing them into a list?

          – Sotos
          Nov 15 '18 at 14:51













          n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

          – Naveed
          Nov 15 '18 at 15:17







          n1<-read.table("data/y1call.txt", sep="") n2<-read.table("data/y2call.txt", sep="") n3<-read.table("data/y3call.txt", sep="") ddf_all<-do.call(rbind,lapply(list(n1,n2,n3), function(i)transform(i,new=rownames(i)))) change<- ddf_all[order(ddf_all$new),]

          – Naveed
          Nov 15 '18 at 15:17















          Well, that does not help me. I do not have the files data/y1call.txt, ... :)

          – Sotos
          Nov 15 '18 at 15:19





          Well, that does not help me. I do not have the files data/y1call.txt, ... :)

          – Sotos
          Nov 15 '18 at 15:19













          Can I get your email to send files and the code? Thanks

          – Naveed
          Nov 15 '18 at 15:23





          Can I get your email to send files and the code? Thanks

          – Naveed
          Nov 15 '18 at 15:23




















          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%2f53300519%2fmerging-multiple-data-frames-by-pasting-alternative-rows%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python