rbind a type of column from a list of tibbles2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameWhy is rbindlist “better” than rbind?Simplest way to get rbind to ignore column namesR: rbind a list of data.frames with different columns in different data framesrbind dataframes from a listR lapply using stringi and rbindrbind and cbind lists togetherrbind all data frames with common names based on list using lapplyR — Cannot rbind In lapply function

How exactly does an Ethernet collision happen in the cable, since nodes use different circuits for Tx and Rx?

Did Amazon pay $0 in taxes last year?

Which situations would cause a company to ground or recall a aircraft series?

Is it possible that a question has only two answers?

Why couldn't the separatists legally leave the Republic?

Source permutation

Professor forcing me to attend a conference, I can't afford even with 50% funding

How can I manipulate the output of Information?

Outlet with 3 sets of wires

How does Ehrenfest's theorem apply to the quantum harmonic oscillator?

PTIJ: Why does only a Shor Tam ask at the Seder, and not a Shor Mu'ad?

Expressing logarithmic equations without logs

Can one live in the U.S. and not use a credit card?

Does "Until when" sound natural for native speakers?

Plausibility of Mushroom Buildings

Signed and unsigned numbers

Why does Central Limit Theorem break down in my simulation?

When a wind turbine does not produce enough electricity how does the power company compensate for the loss?

Would an aboleth's Phantasmal Force lair action be affected by Counterspell, Dispel Magic, and/or Slow?

Windows Server Datacenter Edition - Unlimited Virtual Machines

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

What ability score modifier does a javelin's damage use?

Why does cron require MTA for logging?

Giving a career talk in my old university, how prominently should I tell students my salary?



rbind a type of column from a list of tibbles



2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameWhy is rbindlist “better” than rbind?Simplest way to get rbind to ignore column namesR: rbind a list of data.frames with different columns in different data framesrbind dataframes from a listR lapply using stringi and rbindrbind and cbind lists togetherrbind all data frames with common names based on list using lapplyR — Cannot rbind In lapply function










0















I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
My code is:



 my_files <- list.files(pattern = '*.xlsx')
my_list <- lapply(my_files ,read_excel)


The files contain many different columns:



 lapply(my_list ,colnames)
> lapply(my_list ,colnames)
[[1]]
[1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
[5] "Alt No 2" "Reg No"
[[2]]
[1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
[5] "VehModel" "Last Service Outlet"
[[3]]
[1] "Company Name" "JobCard Branch" "Service_Branch"
[4] "HUB" "Customer Code" "Address"
[7] "Address Line2" "Primary Contact No" "Alt No 1"
[10] "Alt No 2" "Alt No 3" "Zip"
[13] "Source" "City" "Vehicle Model"
[16] "Make" "Reg No" "Chasis No"
[[4]]
[1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
>


Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.










share|improve this question


























    0















    I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
    My code is:



     my_files <- list.files(pattern = '*.xlsx')
    my_list <- lapply(my_files ,read_excel)


    The files contain many different columns:



     lapply(my_list ,colnames)
    > lapply(my_list ,colnames)
    [[1]]
    [1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
    [5] "Alt No 2" "Reg No"
    [[2]]
    [1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
    [5] "VehModel" "Last Service Outlet"
    [[3]]
    [1] "Company Name" "JobCard Branch" "Service_Branch"
    [4] "HUB" "Customer Code" "Address"
    [7] "Address Line2" "Primary Contact No" "Alt No 1"
    [10] "Alt No 2" "Alt No 3" "Zip"
    [13] "Source" "City" "Vehicle Model"
    [16] "Make" "Reg No" "Chasis No"
    [[4]]
    [1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
    >


    Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.










    share|improve this question
























      0












      0








      0








      I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
      My code is:



       my_files <- list.files(pattern = '*.xlsx')
      my_list <- lapply(my_files ,read_excel)


      The files contain many different columns:



       lapply(my_list ,colnames)
      > lapply(my_list ,colnames)
      [[1]]
      [1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
      [5] "Alt No 2" "Reg No"
      [[2]]
      [1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
      [5] "VehModel" "Last Service Outlet"
      [[3]]
      [1] "Company Name" "JobCard Branch" "Service_Branch"
      [4] "HUB" "Customer Code" "Address"
      [7] "Address Line2" "Primary Contact No" "Alt No 1"
      [10] "Alt No 2" "Alt No 3" "Zip"
      [13] "Source" "City" "Vehicle Model"
      [16] "Make" "Reg No" "Chasis No"
      [[4]]
      [1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
      >


      Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.










      share|improve this question














      I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
      My code is:



       my_files <- list.files(pattern = '*.xlsx')
      my_list <- lapply(my_files ,read_excel)


      The files contain many different columns:



       lapply(my_list ,colnames)
      > lapply(my_list ,colnames)
      [[1]]
      [1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
      [5] "Alt No 2" "Reg No"
      [[2]]
      [1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
      [5] "VehModel" "Last Service Outlet"
      [[3]]
      [1] "Company Name" "JobCard Branch" "Service_Branch"
      [4] "HUB" "Customer Code" "Address"
      [7] "Address Line2" "Primary Contact No" "Alt No 1"
      [10] "Alt No 2" "Alt No 3" "Zip"
      [13] "Source" "City" "Vehicle Model"
      [16] "Make" "Reg No" "Chasis No"
      [[4]]
      [1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
      >


      Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.







      r lapply rbind






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 5:23









      Shane BondShane Bond

      724




      724






















          2 Answers
          2






          active

          oldest

          votes


















          0














          We could create a vector of column names (cols) which we want to extract and then use lapply to loop over list of dataframes and subset the columns which match cols.



          cols <- c("Reg No","REG NO#","Reg No","Reg.No")
          data.frame(unlist(lapply(my_list, function(x)
          x[names(x) %in% cols]), use.names = FALSE))


          A reproducible example



          df1 <- data.frame(a = 1:5, b = 2:6)
          df2 <- data.frame(a1 = 1:4, new_s = 2:5)
          df3 <- data.frame(abc = 1:4)
          list_df <- list(df1, df2, df3)


          cols <- c("a", "a1", "abc")
          data.frame(new = unlist(lapply(list_df, function(x)
          x[names(x) %in% cols]),use.names = FALSE))

          # new
          # 1 1
          # 2 2
          # 3 3
          # 4 4
          # 5 5
          # 6 1
          # 7 2
          # 8 3
          # 9 4
          #10 1
          #11 2
          #12 3
          #13 4





          share|improve this answer

























          • I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

            – Shane Bond
            Mar 7 at 5:56







          • 1





            @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

            – Ronak Shah
            Mar 7 at 6:04


















          1














          You may try using grep in case insensitive mode:



          lapply(my_list, function(x) 
          y <- colnames(x)
          y[grep("\breg\b", y, ignore.case=TRUE)]
          )


          This uses the regex pattern breg]b in case insensitive mode, to find the column names which match what you want.






          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%2f55036606%2frbind-a-type-of-column-from-a-list-of-tibbles%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









            0














            We could create a vector of column names (cols) which we want to extract and then use lapply to loop over list of dataframes and subset the columns which match cols.



            cols <- c("Reg No","REG NO#","Reg No","Reg.No")
            data.frame(unlist(lapply(my_list, function(x)
            x[names(x) %in% cols]), use.names = FALSE))


            A reproducible example



            df1 <- data.frame(a = 1:5, b = 2:6)
            df2 <- data.frame(a1 = 1:4, new_s = 2:5)
            df3 <- data.frame(abc = 1:4)
            list_df <- list(df1, df2, df3)


            cols <- c("a", "a1", "abc")
            data.frame(new = unlist(lapply(list_df, function(x)
            x[names(x) %in% cols]),use.names = FALSE))

            # new
            # 1 1
            # 2 2
            # 3 3
            # 4 4
            # 5 5
            # 6 1
            # 7 2
            # 8 3
            # 9 4
            #10 1
            #11 2
            #12 3
            #13 4





            share|improve this answer

























            • I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

              – Shane Bond
              Mar 7 at 5:56







            • 1





              @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

              – Ronak Shah
              Mar 7 at 6:04















            0














            We could create a vector of column names (cols) which we want to extract and then use lapply to loop over list of dataframes and subset the columns which match cols.



            cols <- c("Reg No","REG NO#","Reg No","Reg.No")
            data.frame(unlist(lapply(my_list, function(x)
            x[names(x) %in% cols]), use.names = FALSE))


            A reproducible example



            df1 <- data.frame(a = 1:5, b = 2:6)
            df2 <- data.frame(a1 = 1:4, new_s = 2:5)
            df3 <- data.frame(abc = 1:4)
            list_df <- list(df1, df2, df3)


            cols <- c("a", "a1", "abc")
            data.frame(new = unlist(lapply(list_df, function(x)
            x[names(x) %in% cols]),use.names = FALSE))

            # new
            # 1 1
            # 2 2
            # 3 3
            # 4 4
            # 5 5
            # 6 1
            # 7 2
            # 8 3
            # 9 4
            #10 1
            #11 2
            #12 3
            #13 4





            share|improve this answer

























            • I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

              – Shane Bond
              Mar 7 at 5:56







            • 1





              @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

              – Ronak Shah
              Mar 7 at 6:04













            0












            0








            0







            We could create a vector of column names (cols) which we want to extract and then use lapply to loop over list of dataframes and subset the columns which match cols.



            cols <- c("Reg No","REG NO#","Reg No","Reg.No")
            data.frame(unlist(lapply(my_list, function(x)
            x[names(x) %in% cols]), use.names = FALSE))


            A reproducible example



            df1 <- data.frame(a = 1:5, b = 2:6)
            df2 <- data.frame(a1 = 1:4, new_s = 2:5)
            df3 <- data.frame(abc = 1:4)
            list_df <- list(df1, df2, df3)


            cols <- c("a", "a1", "abc")
            data.frame(new = unlist(lapply(list_df, function(x)
            x[names(x) %in% cols]),use.names = FALSE))

            # new
            # 1 1
            # 2 2
            # 3 3
            # 4 4
            # 5 5
            # 6 1
            # 7 2
            # 8 3
            # 9 4
            #10 1
            #11 2
            #12 3
            #13 4





            share|improve this answer















            We could create a vector of column names (cols) which we want to extract and then use lapply to loop over list of dataframes and subset the columns which match cols.



            cols <- c("Reg No","REG NO#","Reg No","Reg.No")
            data.frame(unlist(lapply(my_list, function(x)
            x[names(x) %in% cols]), use.names = FALSE))


            A reproducible example



            df1 <- data.frame(a = 1:5, b = 2:6)
            df2 <- data.frame(a1 = 1:4, new_s = 2:5)
            df3 <- data.frame(abc = 1:4)
            list_df <- list(df1, df2, df3)


            cols <- c("a", "a1", "abc")
            data.frame(new = unlist(lapply(list_df, function(x)
            x[names(x) %in% cols]),use.names = FALSE))

            # new
            # 1 1
            # 2 2
            # 3 3
            # 4 4
            # 5 5
            # 6 1
            # 7 2
            # 8 3
            # 9 4
            #10 1
            #11 2
            #12 3
            #13 4






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 at 6:03

























            answered Mar 7 at 5:29









            Ronak ShahRonak Shah

            39.8k104162




            39.8k104162












            • I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

              – Shane Bond
              Mar 7 at 5:56







            • 1





              @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

              – Ronak Shah
              Mar 7 at 6:04

















            • I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

              – Shane Bond
              Mar 7 at 5:56







            • 1





              @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

              – Ronak Shah
              Mar 7 at 6:04
















            I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

            – Shane Bond
            Mar 7 at 5:56






            I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4

            – Shane Bond
            Mar 7 at 5:56





            1




            1





            @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

            – Ronak Shah
            Mar 7 at 6:04





            @ShaneBond You could unlist and wrap it in data.frame or tibble according to your preference. Updated the answer.

            – Ronak Shah
            Mar 7 at 6:04













            1














            You may try using grep in case insensitive mode:



            lapply(my_list, function(x) 
            y <- colnames(x)
            y[grep("\breg\b", y, ignore.case=TRUE)]
            )


            This uses the regex pattern breg]b in case insensitive mode, to find the column names which match what you want.






            share|improve this answer



























              1














              You may try using grep in case insensitive mode:



              lapply(my_list, function(x) 
              y <- colnames(x)
              y[grep("\breg\b", y, ignore.case=TRUE)]
              )


              This uses the regex pattern breg]b in case insensitive mode, to find the column names which match what you want.






              share|improve this answer

























                1












                1








                1







                You may try using grep in case insensitive mode:



                lapply(my_list, function(x) 
                y <- colnames(x)
                y[grep("\breg\b", y, ignore.case=TRUE)]
                )


                This uses the regex pattern breg]b in case insensitive mode, to find the column names which match what you want.






                share|improve this answer













                You may try using grep in case insensitive mode:



                lapply(my_list, function(x) 
                y <- colnames(x)
                y[grep("\breg\b", y, ignore.case=TRUE)]
                )


                This uses the regex pattern breg]b in case insensitive mode, to find the column names which match what you want.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 7 at 5:29









                Tim BiegeleisenTim Biegeleisen

                231k1396152




                231k1396152



























                    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%2f55036606%2frbind-a-type-of-column-from-a-list-of-tibbles%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

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived