Implement lapply in conjunction with the get() function to vectorize merge of data tables? R












2















Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?



Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:



ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD") 
for (i in 1:length(ticker_name)) {

dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
dt <- merge(x = dt, y = meta, by= c("id"))
head(dt)
}


My non-working attempt with lapply:



lapply(
X = ticker_name,
FUN =
merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ticker_name)), by = c("id")
))


The error message:



 Error in match.fun(FUN) : 
c("'merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ' is not a function,
character or symbol", "' ticker_name)), by = c("id"))'
is not a function, character or symbol")









share|improve this question





























    2















    Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?



    Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:



    ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD") 
    for (i in 1:length(ticker_name)) {

    dt <- get(paste0("dt_q_", ticker_name[i]))
    meta <- get(paste0("meta_", ticker_name[i]))
    dt <- merge(x = dt, y = meta, by= c("id"))
    head(dt)
    }


    My non-working attempt with lapply:



    lapply(
    X = ticker_name,
    FUN =
    merge(x = get(paste0("dt_q_", ticker_name)),
    y = get(paste0("meta_", ticker_name)), by = c("id")
    ))


    The error message:



     Error in match.fun(FUN) : 
    c("'merge(x = get(paste0("dt_q_", ticker_name)),
    y = get(paste0("meta_", ' is not a function,
    character or symbol", "' ticker_name)), by = c("id"))'
    is not a function, character or symbol")









    share|improve this question



























      2












      2








      2


      1






      Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?



      Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:



      ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD") 
      for (i in 1:length(ticker_name)) {

      dt <- get(paste0("dt_q_", ticker_name[i]))
      meta <- get(paste0("meta_", ticker_name[i]))
      dt <- merge(x = dt, y = meta, by= c("id"))
      head(dt)
      }


      My non-working attempt with lapply:



      lapply(
      X = ticker_name,
      FUN =
      merge(x = get(paste0("dt_q_", ticker_name)),
      y = get(paste0("meta_", ticker_name)), by = c("id")
      ))


      The error message:



       Error in match.fun(FUN) : 
      c("'merge(x = get(paste0("dt_q_", ticker_name)),
      y = get(paste0("meta_", ' is not a function,
      character or symbol", "' ticker_name)), by = c("id"))'
      is not a function, character or symbol")









      share|improve this question
















      Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?



      Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:



      ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD") 
      for (i in 1:length(ticker_name)) {

      dt <- get(paste0("dt_q_", ticker_name[i]))
      meta <- get(paste0("meta_", ticker_name[i]))
      dt <- merge(x = dt, y = meta, by= c("id"))
      head(dt)
      }


      My non-working attempt with lapply:



      lapply(
      X = ticker_name,
      FUN =
      merge(x = get(paste0("dt_q_", ticker_name)),
      y = get(paste0("meta_", ticker_name)), by = c("id")
      ))


      The error message:



       Error in match.fun(FUN) : 
      c("'merge(x = get(paste0("dt_q_", ticker_name)),
      y = get(paste0("meta_", ' is not a function,
      character or symbol", "' ticker_name)), by = c("id"))'
      is not a function, character or symbol")






      r merge get vectorization lapply






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 2:28







      k1000x

















      asked Nov 26 '18 at 22:27









      k1000xk1000x

      386




      386
























          1 Answer
          1






          active

          oldest

          votes


















          2














          We can use mget to return all the objects into a list and as the corresponding data.table should be merged, use Map which can have multiple arguments



          Map(merge, mget(paste0("dt_q_", ticker_name)), 
          mget(paste0("meta_", ticker_name)),
          MoreArgs = list(by = 'id'))




          Or using lapply, loop through the 'ticker_name' then paste the corresponding 'prefix' part, get the values of the string objects and merge



          lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)), 
          get(paste0("meta_", x)), by = 'id'))


          NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is pasteing the prefix with the whole 'ticker_name' which is not the case if we check the for loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence



          lapply(seq_along(ticker_name), function(i) {
          dt <- get(paste0("dt_q_", ticker_name[i]))
          meta <- get(paste0("meta_", ticker_name[i]))
          merge(x = dt, y = meta, by= "id")

          })





          share|improve this answer





















          • 1





            Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

            – k1000x
            Nov 27 '18 at 2:27






          • 1





            @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

            – akrun
            Nov 27 '18 at 3:20











          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%2f53490069%2fimplement-lapply-in-conjunction-with-the-get-function-to-vectorize-merge-of-da%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









          2














          We can use mget to return all the objects into a list and as the corresponding data.table should be merged, use Map which can have multiple arguments



          Map(merge, mget(paste0("dt_q_", ticker_name)), 
          mget(paste0("meta_", ticker_name)),
          MoreArgs = list(by = 'id'))




          Or using lapply, loop through the 'ticker_name' then paste the corresponding 'prefix' part, get the values of the string objects and merge



          lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)), 
          get(paste0("meta_", x)), by = 'id'))


          NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is pasteing the prefix with the whole 'ticker_name' which is not the case if we check the for loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence



          lapply(seq_along(ticker_name), function(i) {
          dt <- get(paste0("dt_q_", ticker_name[i]))
          meta <- get(paste0("meta_", ticker_name[i]))
          merge(x = dt, y = meta, by= "id")

          })





          share|improve this answer





















          • 1





            Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

            – k1000x
            Nov 27 '18 at 2:27






          • 1





            @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

            – akrun
            Nov 27 '18 at 3:20
















          2














          We can use mget to return all the objects into a list and as the corresponding data.table should be merged, use Map which can have multiple arguments



          Map(merge, mget(paste0("dt_q_", ticker_name)), 
          mget(paste0("meta_", ticker_name)),
          MoreArgs = list(by = 'id'))




          Or using lapply, loop through the 'ticker_name' then paste the corresponding 'prefix' part, get the values of the string objects and merge



          lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)), 
          get(paste0("meta_", x)), by = 'id'))


          NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is pasteing the prefix with the whole 'ticker_name' which is not the case if we check the for loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence



          lapply(seq_along(ticker_name), function(i) {
          dt <- get(paste0("dt_q_", ticker_name[i]))
          meta <- get(paste0("meta_", ticker_name[i]))
          merge(x = dt, y = meta, by= "id")

          })





          share|improve this answer





















          • 1





            Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

            – k1000x
            Nov 27 '18 at 2:27






          • 1





            @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

            – akrun
            Nov 27 '18 at 3:20














          2












          2








          2







          We can use mget to return all the objects into a list and as the corresponding data.table should be merged, use Map which can have multiple arguments



          Map(merge, mget(paste0("dt_q_", ticker_name)), 
          mget(paste0("meta_", ticker_name)),
          MoreArgs = list(by = 'id'))




          Or using lapply, loop through the 'ticker_name' then paste the corresponding 'prefix' part, get the values of the string objects and merge



          lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)), 
          get(paste0("meta_", x)), by = 'id'))


          NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is pasteing the prefix with the whole 'ticker_name' which is not the case if we check the for loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence



          lapply(seq_along(ticker_name), function(i) {
          dt <- get(paste0("dt_q_", ticker_name[i]))
          meta <- get(paste0("meta_", ticker_name[i]))
          merge(x = dt, y = meta, by= "id")

          })





          share|improve this answer















          We can use mget to return all the objects into a list and as the corresponding data.table should be merged, use Map which can have multiple arguments



          Map(merge, mget(paste0("dt_q_", ticker_name)), 
          mget(paste0("meta_", ticker_name)),
          MoreArgs = list(by = 'id'))




          Or using lapply, loop through the 'ticker_name' then paste the corresponding 'prefix' part, get the values of the string objects and merge



          lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)), 
          get(paste0("meta_", x)), by = 'id'))


          NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is pasteing the prefix with the whole 'ticker_name' which is not the case if we check the for loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence



          lapply(seq_along(ticker_name), function(i) {
          dt <- get(paste0("dt_q_", ticker_name[i]))
          meta <- get(paste0("meta_", ticker_name[i]))
          merge(x = dt, y = meta, by= "id")

          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 22:44

























          answered Nov 26 '18 at 22:36









          akrunakrun

          409k13198273




          409k13198273








          • 1





            Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

            – k1000x
            Nov 27 '18 at 2:27






          • 1





            @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

            – akrun
            Nov 27 '18 at 3:20














          • 1





            Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

            – k1000x
            Nov 27 '18 at 2:27






          • 1





            @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

            – akrun
            Nov 27 '18 at 3:20








          1




          1





          Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

          – k1000x
          Nov 27 '18 at 2:27





          Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?

          – k1000x
          Nov 27 '18 at 2:27




          1




          1





          @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

          – akrun
          Nov 27 '18 at 3:20





          @k1000x Thank you. In Map we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name))). That would make it easier to understand. The compact method used in Map specifies the arguments of merge (by = 'id'). When we place it inside list, it will be a single unit that can be recycled for each element of the list (and it is the same argument for all the elements)

          – akrun
          Nov 27 '18 at 3:20




















          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%2f53490069%2fimplement-lapply-in-conjunction-with-the-get-function-to-vectorize-merge-of-da%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)