Comparing Column names in R across various data frames












1














I am currently try to compare the column classes and names of various data frames in R prior to undertaking any transformations and calculations.
The code I have is noted below::



library(dplyr)
m1 <- mtcars
m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))


If someone can solve this for dataframes stored in a list, that would be great. All my dataframes are currently stored in a list, for easier processing.



All.list <- list(m1,m2,m3)


I am expecting that the output is displayed in a matrix form as shown in the dataframe "out". The output in "out" is not desireable as it is incorrect. I am expecting the output to be more along the following::



enter image description here










share|improve this question



























    1














    I am currently try to compare the column classes and names of various data frames in R prior to undertaking any transformations and calculations.
    The code I have is noted below::



    library(dplyr)
    m1 <- mtcars
    m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
    m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

    out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))


    If someone can solve this for dataframes stored in a list, that would be great. All my dataframes are currently stored in a list, for easier processing.



    All.list <- list(m1,m2,m3)


    I am expecting that the output is displayed in a matrix form as shown in the dataframe "out". The output in "out" is not desireable as it is incorrect. I am expecting the output to be more along the following::



    enter image description here










    share|improve this question

























      1












      1








      1







      I am currently try to compare the column classes and names of various data frames in R prior to undertaking any transformations and calculations.
      The code I have is noted below::



      library(dplyr)
      m1 <- mtcars
      m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
      m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

      out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))


      If someone can solve this for dataframes stored in a list, that would be great. All my dataframes are currently stored in a list, for easier processing.



      All.list <- list(m1,m2,m3)


      I am expecting that the output is displayed in a matrix form as shown in the dataframe "out". The output in "out" is not desireable as it is incorrect. I am expecting the output to be more along the following::



      enter image description here










      share|improve this question













      I am currently try to compare the column classes and names of various data frames in R prior to undertaking any transformations and calculations.
      The code I have is noted below::



      library(dplyr)
      m1 <- mtcars
      m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
      m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

      out <- cbind(sapply(m1, class), sapply(m2, class), sapply(m3, class))


      If someone can solve this for dataframes stored in a list, that would be great. All my dataframes are currently stored in a list, for easier processing.



      All.list <- list(m1,m2,m3)


      I am expecting that the output is displayed in a matrix form as shown in the dataframe "out". The output in "out" is not desireable as it is incorrect. I am expecting the output to be more along the following::



      enter image description here







      r class dataframe lapply sapply






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 15:10









      Seb

      1488




      1488
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I think the easiest way would be to define a function, and then use a combination of lapply and dplyr to obtain the result you want. Here is how I did it.



          library(dplyr)
          m1 <- mtcars
          m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
          m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

          All.list <- list(m1,m2,m3)


          ##Define a function to get variable names and types
          my_function <- function(data_frame){
          require(dplyr)
          x <- tibble(`var_name` = colnames(data_frame),
          `var_type` = sapply(data_frame, class))
          return(x)
          }


          target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
          mutate(element =i)) %>%
          bind_rows() %>%
          spread(element, var_type)

          target





          share|improve this answer

















          • 1




            thank you - this works perfectly
            – Seb
            Nov 12 '18 at 18:36











          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%2f53264993%2fcomparing-column-names-in-r-across-various-data-frames%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









          1














          I think the easiest way would be to define a function, and then use a combination of lapply and dplyr to obtain the result you want. Here is how I did it.



          library(dplyr)
          m1 <- mtcars
          m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
          m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

          All.list <- list(m1,m2,m3)


          ##Define a function to get variable names and types
          my_function <- function(data_frame){
          require(dplyr)
          x <- tibble(`var_name` = colnames(data_frame),
          `var_type` = sapply(data_frame, class))
          return(x)
          }


          target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
          mutate(element =i)) %>%
          bind_rows() %>%
          spread(element, var_type)

          target





          share|improve this answer

















          • 1




            thank you - this works perfectly
            – Seb
            Nov 12 '18 at 18:36
















          1














          I think the easiest way would be to define a function, and then use a combination of lapply and dplyr to obtain the result you want. Here is how I did it.



          library(dplyr)
          m1 <- mtcars
          m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
          m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

          All.list <- list(m1,m2,m3)


          ##Define a function to get variable names and types
          my_function <- function(data_frame){
          require(dplyr)
          x <- tibble(`var_name` = colnames(data_frame),
          `var_type` = sapply(data_frame, class))
          return(x)
          }


          target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
          mutate(element =i)) %>%
          bind_rows() %>%
          spread(element, var_type)

          target





          share|improve this answer

















          • 1




            thank you - this works perfectly
            – Seb
            Nov 12 '18 at 18:36














          1












          1








          1






          I think the easiest way would be to define a function, and then use a combination of lapply and dplyr to obtain the result you want. Here is how I did it.



          library(dplyr)
          m1 <- mtcars
          m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
          m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

          All.list <- list(m1,m2,m3)


          ##Define a function to get variable names and types
          my_function <- function(data_frame){
          require(dplyr)
          x <- tibble(`var_name` = colnames(data_frame),
          `var_type` = sapply(data_frame, class))
          return(x)
          }


          target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
          mutate(element =i)) %>%
          bind_rows() %>%
          spread(element, var_type)

          target





          share|improve this answer












          I think the easiest way would be to define a function, and then use a combination of lapply and dplyr to obtain the result you want. Here is how I did it.



          library(dplyr)
          m1 <- mtcars
          m2 <- mtcars %>% mutate(cyl = factor(cyl), xxxx1 = factor(cyl))
          m3 <- mtcars %>% mutate(cyl = factor(cyl), xxxx2 = factor(cyl))

          All.list <- list(m1,m2,m3)


          ##Define a function to get variable names and types
          my_function <- function(data_frame){
          require(dplyr)
          x <- tibble(`var_name` = colnames(data_frame),
          `var_type` = sapply(data_frame, class))
          return(x)
          }


          target <- lapply(1:length(All.list),function(i)my_function(All.list[[i]]) %>%
          mutate(element =i)) %>%
          bind_rows() %>%
          spread(element, var_type)

          target






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 15:23









          Harro Cyranka

          1,1601513




          1,1601513








          • 1




            thank you - this works perfectly
            – Seb
            Nov 12 '18 at 18:36














          • 1




            thank you - this works perfectly
            – Seb
            Nov 12 '18 at 18:36








          1




          1




          thank you - this works perfectly
          – Seb
          Nov 12 '18 at 18:36




          thank you - this works perfectly
          – Seb
          Nov 12 '18 at 18:36


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53264993%2fcomparing-column-names-in-r-across-various-data-frames%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma