How to apply a mathematical function to a list of objects in R?











up vote
-2
down vote

favorite












So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.



Here's a small example.



a = 680

b = 820

c = 1040

list = as.list(ls())

fun = function(x){x*.8}

for (i in list){
fun(i)
}


And I get an error saying non-numeric argument to binary operator. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.



Thank you!!










share|improve this question


















  • 2




    change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
    – Onyambu
    Nov 19 at 18:25










  • This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
    – D. Rodríguez
    Nov 19 at 18:29















up vote
-2
down vote

favorite












So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.



Here's a small example.



a = 680

b = 820

c = 1040

list = as.list(ls())

fun = function(x){x*.8}

for (i in list){
fun(i)
}


And I get an error saying non-numeric argument to binary operator. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.



Thank you!!










share|improve this question


















  • 2




    change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
    – Onyambu
    Nov 19 at 18:25










  • This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
    – D. Rodríguez
    Nov 19 at 18:29













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.



Here's a small example.



a = 680

b = 820

c = 1040

list = as.list(ls())

fun = function(x){x*.8}

for (i in list){
fun(i)
}


And I get an error saying non-numeric argument to binary operator. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.



Thank you!!










share|improve this question













So I have a large list of objects (200+) which only contain a numeric value. I want to apply the same mathematical (get the 80% of the value) function for all of them, but it's not working out for me.



Here's a small example.



a = 680

b = 820

c = 1040

list = as.list(ls())

fun = function(x){x*.8}

for (i in list){
fun(i)
}


And I get an error saying non-numeric argument to binary operator. I get that this might be happening because the software is applying the function to the names in the list and no the numerical values, but for the life of me I can't make it work and haven't been able to find this exact issue online. I know the answer must be so simple but I'm a newbie, really. Any help is appreciated.



Thank you!!







r list function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 18:05









D. Rodríguez

12




12








  • 2




    change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
    – Onyambu
    Nov 19 at 18:25










  • This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
    – D. Rodríguez
    Nov 19 at 18:29














  • 2




    change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
    – Onyambu
    Nov 19 at 18:25










  • This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
    – D. Rodríguez
    Nov 19 at 18:29








2




2




change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
– Onyambu
Nov 19 at 18:25




change your list to list = mget(ls()) instead of as.list use mget then you can be able to do all the other operations
– Onyambu
Nov 19 at 18:25












This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29




This is it. I appreciate Jason's and vectorson's answers, but having 200+ elements in my environment I don't want the create the vector by hand. I didn't know the "mget" function. Thank you so much!
– D. Rodríguez
Nov 19 at 18:29












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










You are looking for the mget function: This is a multivariate format of get



thus:



a = 680

b = 820

c = 1040

list = mget(ls())## This is the part you need to change

fun = function(x){x*.8}

for (i in list){
fun(i)
}





share|improve this answer





















  • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
    – D. Rodríguez
    Nov 19 at 18:34


















up vote
1
down vote













You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")



Having said that, this example could be more easily be done using regular vector and scalar multiplication.



x = c(680, 820, 1040)
x * .8


If for some reason you want you use a list and function anyway I would recommend using lapply, like so:



a = 680
b = 820
c = 1040

list = list(a, b, c)

fun = function(x){ x*.8 }

lapply(list, fun)





share|improve this answer




























    up vote
    1
    down vote













    Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:



    ##### Solution 1 using eapply() #####
    # Build a new temporary environment to save all numeric variables
    tempEnv <- new.env()
    tempEnv$a = 680
    tempEnv$b = 820
    tempEnv$c = 1040

    fun = function(x){x*.8}

    # apply fun() to all variables in "tempEnv"
    eapply(tempEnv, fun)


    ##### Solution 2 using lapply() (Recommended) #####
    # Define all variables in a list
    myList <- list(a = 680,
    b = 820,
    c = 1040)

    # apply fun() to all values in myList
    lapply(myList, fun)





    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',
      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%2f53380325%2fhow-to-apply-a-mathematical-function-to-a-list-of-objects-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








      up vote
      0
      down vote



      accepted










      You are looking for the mget function: This is a multivariate format of get



      thus:



      a = 680

      b = 820

      c = 1040

      list = mget(ls())## This is the part you need to change

      fun = function(x){x*.8}

      for (i in list){
      fun(i)
      }





      share|improve this answer





















      • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
        – D. Rodríguez
        Nov 19 at 18:34















      up vote
      0
      down vote



      accepted










      You are looking for the mget function: This is a multivariate format of get



      thus:



      a = 680

      b = 820

      c = 1040

      list = mget(ls())## This is the part you need to change

      fun = function(x){x*.8}

      for (i in list){
      fun(i)
      }





      share|improve this answer





















      • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
        – D. Rodríguez
        Nov 19 at 18:34













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      You are looking for the mget function: This is a multivariate format of get



      thus:



      a = 680

      b = 820

      c = 1040

      list = mget(ls())## This is the part you need to change

      fun = function(x){x*.8}

      for (i in list){
      fun(i)
      }





      share|improve this answer












      You are looking for the mget function: This is a multivariate format of get



      thus:



      a = 680

      b = 820

      c = 1040

      list = mget(ls())## This is the part you need to change

      fun = function(x){x*.8}

      for (i in list){
      fun(i)
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 19 at 18:33









      Onyambu

      15.3k1519




      15.3k1519












      • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
        – D. Rodríguez
        Nov 19 at 18:34


















      • This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
        – D. Rodríguez
        Nov 19 at 18:34
















      This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
      – D. Rodríguez
      Nov 19 at 18:34




      This is it! Thank you Onyambu. Can't upvote yet (too new), but this was the solution for me.
      – D. Rodríguez
      Nov 19 at 18:34












      up vote
      1
      down vote













      You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")



      Having said that, this example could be more easily be done using regular vector and scalar multiplication.



      x = c(680, 820, 1040)
      x * .8


      If for some reason you want you use a list and function anyway I would recommend using lapply, like so:



      a = 680
      b = 820
      c = 1040

      list = list(a, b, c)

      fun = function(x){ x*.8 }

      lapply(list, fun)





      share|improve this answer

























        up vote
        1
        down vote













        You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")



        Having said that, this example could be more easily be done using regular vector and scalar multiplication.



        x = c(680, 820, 1040)
        x * .8


        If for some reason you want you use a list and function anyway I would recommend using lapply, like so:



        a = 680
        b = 820
        c = 1040

        list = list(a, b, c)

        fun = function(x){ x*.8 }

        lapply(list, fun)





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")



          Having said that, this example could be more easily be done using regular vector and scalar multiplication.



          x = c(680, 820, 1040)
          x * .8


          If for some reason you want you use a list and function anyway I would recommend using lapply, like so:



          a = 680
          b = 820
          c = 1040

          list = list(a, b, c)

          fun = function(x){ x*.8 }

          lapply(list, fun)





          share|improve this answer












          You get an error because ls() only returns the names of the variables you've defined. ("a", "b" and "c")



          Having said that, this example could be more easily be done using regular vector and scalar multiplication.



          x = c(680, 820, 1040)
          x * .8


          If for some reason you want you use a list and function anyway I would recommend using lapply, like so:



          a = 680
          b = 820
          c = 1040

          list = list(a, b, c)

          fun = function(x){ x*.8 }

          lapply(list, fun)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 18:19









          vectorson

          193




          193






















              up vote
              1
              down vote













              Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:



              ##### Solution 1 using eapply() #####
              # Build a new temporary environment to save all numeric variables
              tempEnv <- new.env()
              tempEnv$a = 680
              tempEnv$b = 820
              tempEnv$c = 1040

              fun = function(x){x*.8}

              # apply fun() to all variables in "tempEnv"
              eapply(tempEnv, fun)


              ##### Solution 2 using lapply() (Recommended) #####
              # Define all variables in a list
              myList <- list(a = 680,
              b = 820,
              c = 1040)

              # apply fun() to all values in myList
              lapply(myList, fun)





              share|improve this answer



























                up vote
                1
                down vote













                Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:



                ##### Solution 1 using eapply() #####
                # Build a new temporary environment to save all numeric variables
                tempEnv <- new.env()
                tempEnv$a = 680
                tempEnv$b = 820
                tempEnv$c = 1040

                fun = function(x){x*.8}

                # apply fun() to all variables in "tempEnv"
                eapply(tempEnv, fun)


                ##### Solution 2 using lapply() (Recommended) #####
                # Define all variables in a list
                myList <- list(a = 680,
                b = 820,
                c = 1040)

                # apply fun() to all values in myList
                lapply(myList, fun)





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:



                  ##### Solution 1 using eapply() #####
                  # Build a new temporary environment to save all numeric variables
                  tempEnv <- new.env()
                  tempEnv$a = 680
                  tempEnv$b = 820
                  tempEnv$c = 1040

                  fun = function(x){x*.8}

                  # apply fun() to all variables in "tempEnv"
                  eapply(tempEnv, fun)


                  ##### Solution 2 using lapply() (Recommended) #####
                  # Define all variables in a list
                  myList <- list(a = 680,
                  b = 820,
                  c = 1040)

                  # apply fun() to all values in myList
                  lapply(myList, fun)





                  share|improve this answer














                  Use eapply to apply a function to all variables in an environment. However, I recommend you to first build a list of numeric variables and use lapply(myList, fun) as variables in an environment can easily be manipulated unintentionally. Refer to the code below:



                  ##### Solution 1 using eapply() #####
                  # Build a new temporary environment to save all numeric variables
                  tempEnv <- new.env()
                  tempEnv$a = 680
                  tempEnv$b = 820
                  tempEnv$c = 1040

                  fun = function(x){x*.8}

                  # apply fun() to all variables in "tempEnv"
                  eapply(tempEnv, fun)


                  ##### Solution 2 using lapply() (Recommended) #####
                  # Define all variables in a list
                  myList <- list(a = 680,
                  b = 820,
                  c = 1040)

                  # apply fun() to all values in myList
                  lapply(myList, fun)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 at 22:13

























                  answered Nov 19 at 18:19









                  Jason Jisu Park

                  1266




                  1266






























                      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%2f53380325%2fhow-to-apply-a-mathematical-function-to-a-list-of-objects-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

                      Wiesbaden

                      Marschland

                      Dieringhausen