mathematically manipulate specific elements of vector/object in R










1















I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?



for example, if my data is defined as:



numbers = 1:500
data= sample(numbers)


I now have a random list of numbers between 1 and 500.



I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:



for (i in 1:length(data)) 
if data[i]>200
data - 360
else
data - 0




This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!










share|improve this question


























    1















    I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?



    for example, if my data is defined as:



    numbers = 1:500
    data= sample(numbers)


    I now have a random list of numbers between 1 and 500.



    I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:



    for (i in 1:length(data)) 
    if data[i]>200
    data - 360
    else
    data - 0




    This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!










    share|improve this question
























      1












      1








      1








      I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?



      for example, if my data is defined as:



      numbers = 1:500
      data= sample(numbers)


      I now have a random list of numbers between 1 and 500.



      I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:



      for (i in 1:length(data)) 
      if data[i]>200
      data - 360
      else
      data - 0




      This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!










      share|improve this question














      I'm wondering how I can subtract a specific value from elements in a vector that are greater than a threshold I set?



      for example, if my data is defined as:



      numbers = 1:500
      data= sample(numbers)


      I now have a random list of numbers between 1 and 500.



      I now want to subtract 360 from each value in this vector that is greater than 200. Logically i want to write a for loop with an if statement to do this. I have gone as far to write code that looks like this:



      for (i in 1:length(data)) 
      if data[i]>200
      data - 360
      else
      data - 0




      This clearly does not work but I am stumped as to what I can do to achieve my goal. Since I will need to plot these data afterwards, I need them to stay in their original order within the output vector. Thanks a ton for the help!







      r if-statement vector subtract






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 1:23









      user1554925user1554925

      203




      203






















          4 Answers
          4






          active

          oldest

          votes


















          1














          ifelse() is perfect for the purpose, you can use the data vector of your sample:



          data <- ifelse( data > 200, data - 360, data )


          So merely to give another taste:



          set.seed( 1110 ) # make it reproducible
          data <- sample( numbers )
          head( data, 10 )
          [1] 242 395 440 287 110 46 241 489 276 178
          data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
          head( data )
          [1] -118 35 80 -73 110 46 -119 129 -84 178


          Your loop would have worked as well after correcting some mistakes, see below:



          for (i in 1:length(data)) 
          if( data[i]>200 )
          data[ i ] <- data[ i ] - 360






          share|improve this answer

























          • THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

            – user1554925
            Nov 13 '18 at 20:42


















          1














          Well a simple answer is this



          x[x>200] = x[x>200] - 360



          1. x>200: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE

          2. Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector

          Your code is wrong because you are using wrong the operator [. It must throw an error.



          for (i in 1:length(data)) 
          if (data[i]>200)
          data[i] = data[i] - 360




          This is the correct way. You must read R from the start to understand better the operators...






          share|improve this answer






























            1














            There is no need to use loops. Here's the simplest way -



            data <- data - 360*(data > 200)


            Demo -



            set.seed(1)
            numbers <- 1:500
            data <- sample(numbers)

            head(data)
            # [1] 133 186 286 452 101 445

            data <- data - 360*(data > 200)

            head(data)
            # [1] 133 186 -74 92 101 85





            share|improve this answer






























              0














              I would use ifelse



              numbers = 1:500
              data= sample(numbers)

              new_numbers <- ifelse(numbers >200,numbers-360, numbers)
              new_numbers





              share|improve this answer























              • He want to substract only the numbers that are greater than 200 and all the others to be the same...

                – Csd
                Nov 13 '18 at 1:41










              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%2f53272432%2fmathematically-manipulate-specific-elements-of-vector-object-in-r%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              ifelse() is perfect for the purpose, you can use the data vector of your sample:



              data <- ifelse( data > 200, data - 360, data )


              So merely to give another taste:



              set.seed( 1110 ) # make it reproducible
              data <- sample( numbers )
              head( data, 10 )
              [1] 242 395 440 287 110 46 241 489 276 178
              data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
              head( data )
              [1] -118 35 80 -73 110 46 -119 129 -84 178


              Your loop would have worked as well after correcting some mistakes, see below:



              for (i in 1:length(data)) 
              if( data[i]>200 )
              data[ i ] <- data[ i ] - 360






              share|improve this answer

























              • THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

                – user1554925
                Nov 13 '18 at 20:42















              1














              ifelse() is perfect for the purpose, you can use the data vector of your sample:



              data <- ifelse( data > 200, data - 360, data )


              So merely to give another taste:



              set.seed( 1110 ) # make it reproducible
              data <- sample( numbers )
              head( data, 10 )
              [1] 242 395 440 287 110 46 241 489 276 178
              data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
              head( data )
              [1] -118 35 80 -73 110 46 -119 129 -84 178


              Your loop would have worked as well after correcting some mistakes, see below:



              for (i in 1:length(data)) 
              if( data[i]>200 )
              data[ i ] <- data[ i ] - 360






              share|improve this answer

























              • THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

                – user1554925
                Nov 13 '18 at 20:42













              1












              1








              1







              ifelse() is perfect for the purpose, you can use the data vector of your sample:



              data <- ifelse( data > 200, data - 360, data )


              So merely to give another taste:



              set.seed( 1110 ) # make it reproducible
              data <- sample( numbers )
              head( data, 10 )
              [1] 242 395 440 287 110 46 241 489 276 178
              data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
              head( data )
              [1] -118 35 80 -73 110 46 -119 129 -84 178


              Your loop would have worked as well after correcting some mistakes, see below:



              for (i in 1:length(data)) 
              if( data[i]>200 )
              data[ i ] <- data[ i ] - 360






              share|improve this answer















              ifelse() is perfect for the purpose, you can use the data vector of your sample:



              data <- ifelse( data > 200, data - 360, data )


              So merely to give another taste:



              set.seed( 1110 ) # make it reproducible
              data <- sample( numbers )
              head( data, 10 )
              [1] 242 395 440 287 110 46 241 489 276 178
              data[ data > 200 ] <- data[ data > 200 ] - 360 # replace inline
              head( data )
              [1] -118 35 80 -73 110 46 -119 129 -84 178


              Your loop would have worked as well after correcting some mistakes, see below:



              for (i in 1:length(data)) 
              if( data[i]>200 )
              data[ i ] <- data[ i ] - 360







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '18 at 1:54

























              answered Nov 13 '18 at 1:47









              vaettchenvaettchen

              5,2901332




              5,2901332












              • THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

                – user1554925
                Nov 13 '18 at 20:42

















              • THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

                – user1554925
                Nov 13 '18 at 20:42
















              THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

              – user1554925
              Nov 13 '18 at 20:42





              THANK YOU SO MUCH!!! The first one works PERFECTLY! Good to know about such a simple, useful function.

              – user1554925
              Nov 13 '18 at 20:42













              1














              Well a simple answer is this



              x[x>200] = x[x>200] - 360



              1. x>200: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE

              2. Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector

              Your code is wrong because you are using wrong the operator [. It must throw an error.



              for (i in 1:length(data)) 
              if (data[i]>200)
              data[i] = data[i] - 360




              This is the correct way. You must read R from the start to understand better the operators...






              share|improve this answer



























                1














                Well a simple answer is this



                x[x>200] = x[x>200] - 360



                1. x>200: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE

                2. Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector

                Your code is wrong because you are using wrong the operator [. It must throw an error.



                for (i in 1:length(data)) 
                if (data[i]>200)
                data[i] = data[i] - 360




                This is the correct way. You must read R from the start to understand better the operators...






                share|improve this answer

























                  1












                  1








                  1







                  Well a simple answer is this



                  x[x>200] = x[x>200] - 360



                  1. x>200: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE

                  2. Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector

                  Your code is wrong because you are using wrong the operator [. It must throw an error.



                  for (i in 1:length(data)) 
                  if (data[i]>200)
                  data[i] = data[i] - 360




                  This is the correct way. You must read R from the start to understand better the operators...






                  share|improve this answer













                  Well a simple answer is this



                  x[x>200] = x[x>200] - 360



                  1. x>200: return a logical vector were each value thas is greater than 200 is TRUE and other FALSE

                  2. Use the logical vector to access the element of your initial vector. Assign or access only the element that has a TRUE value in the logical vector

                  Your code is wrong because you are using wrong the operator [. It must throw an error.



                  for (i in 1:length(data)) 
                  if (data[i]>200)
                  data[i] = data[i] - 360




                  This is the correct way. You must read R from the start to understand better the operators...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 1:32









                  CsdCsd

                  31819




                  31819





















                      1














                      There is no need to use loops. Here's the simplest way -



                      data <- data - 360*(data > 200)


                      Demo -



                      set.seed(1)
                      numbers <- 1:500
                      data <- sample(numbers)

                      head(data)
                      # [1] 133 186 286 452 101 445

                      data <- data - 360*(data > 200)

                      head(data)
                      # [1] 133 186 -74 92 101 85





                      share|improve this answer



























                        1














                        There is no need to use loops. Here's the simplest way -



                        data <- data - 360*(data > 200)


                        Demo -



                        set.seed(1)
                        numbers <- 1:500
                        data <- sample(numbers)

                        head(data)
                        # [1] 133 186 286 452 101 445

                        data <- data - 360*(data > 200)

                        head(data)
                        # [1] 133 186 -74 92 101 85





                        share|improve this answer

























                          1












                          1








                          1







                          There is no need to use loops. Here's the simplest way -



                          data <- data - 360*(data > 200)


                          Demo -



                          set.seed(1)
                          numbers <- 1:500
                          data <- sample(numbers)

                          head(data)
                          # [1] 133 186 286 452 101 445

                          data <- data - 360*(data > 200)

                          head(data)
                          # [1] 133 186 -74 92 101 85





                          share|improve this answer













                          There is no need to use loops. Here's the simplest way -



                          data <- data - 360*(data > 200)


                          Demo -



                          set.seed(1)
                          numbers <- 1:500
                          data <- sample(numbers)

                          head(data)
                          # [1] 133 186 286 452 101 445

                          data <- data - 360*(data > 200)

                          head(data)
                          # [1] 133 186 -74 92 101 85






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 2:20









                          ShreeShree

                          3,4911424




                          3,4911424





















                              0














                              I would use ifelse



                              numbers = 1:500
                              data= sample(numbers)

                              new_numbers <- ifelse(numbers >200,numbers-360, numbers)
                              new_numbers





                              share|improve this answer























                              • He want to substract only the numbers that are greater than 200 and all the others to be the same...

                                – Csd
                                Nov 13 '18 at 1:41















                              0














                              I would use ifelse



                              numbers = 1:500
                              data= sample(numbers)

                              new_numbers <- ifelse(numbers >200,numbers-360, numbers)
                              new_numbers





                              share|improve this answer























                              • He want to substract only the numbers that are greater than 200 and all the others to be the same...

                                – Csd
                                Nov 13 '18 at 1:41













                              0












                              0








                              0







                              I would use ifelse



                              numbers = 1:500
                              data= sample(numbers)

                              new_numbers <- ifelse(numbers >200,numbers-360, numbers)
                              new_numbers





                              share|improve this answer













                              I would use ifelse



                              numbers = 1:500
                              data= sample(numbers)

                              new_numbers <- ifelse(numbers >200,numbers-360, numbers)
                              new_numbers






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 13 '18 at 1:25









                              Harro CyrankaHarro Cyranka

                              1,6181615




                              1,6181615












                              • He want to substract only the numbers that are greater than 200 and all the others to be the same...

                                – Csd
                                Nov 13 '18 at 1:41

















                              • He want to substract only the numbers that are greater than 200 and all the others to be the same...

                                – Csd
                                Nov 13 '18 at 1:41
















                              He want to substract only the numbers that are greater than 200 and all the others to be the same...

                              – Csd
                              Nov 13 '18 at 1:41





                              He want to substract only the numbers that are greater than 200 and all the others to be the same...

                              – Csd
                              Nov 13 '18 at 1:41

















                              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%2f53272432%2fmathematically-manipulate-specific-elements-of-vector-object-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

                              𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                              Crossroads (UK TV series)

                              ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế