Why for loop does not change element in a numeric vector? [duplicate]












-2
















This question already has an answer here:




  • Replace given value in vector

    5 answers




Element in a numeric vector does not change after running a for loop that iterates each element.



I have a numeric vector:



>str(df$Grad.Rate)
num [1:777] 60 56 54 59 15 55 63 73 80 52 ...


I want to update any element>100



> for (i in df$Grad.Rate){
+ if (i >100){
+ print(i)
+ i = 100
+ print(paste0('changed to ', i))
+ }
+ }
[1] 118
[1] "changed to 100"


After I run the for loop, the element that is >100 is still in the vector



> any(df$Grad.Rate>100)
[1] TRUE


Why?










share|improve this question















marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 16:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

    – akrun
    Dec 31 '18 at 14:49


















-2
















This question already has an answer here:




  • Replace given value in vector

    5 answers




Element in a numeric vector does not change after running a for loop that iterates each element.



I have a numeric vector:



>str(df$Grad.Rate)
num [1:777] 60 56 54 59 15 55 63 73 80 52 ...


I want to update any element>100



> for (i in df$Grad.Rate){
+ if (i >100){
+ print(i)
+ i = 100
+ print(paste0('changed to ', i))
+ }
+ }
[1] 118
[1] "changed to 100"


After I run the for loop, the element that is >100 is still in the vector



> any(df$Grad.Rate>100)
[1] TRUE


Why?










share|improve this question















marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 16:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

    – akrun
    Dec 31 '18 at 14:49
















-2












-2








-2









This question already has an answer here:




  • Replace given value in vector

    5 answers




Element in a numeric vector does not change after running a for loop that iterates each element.



I have a numeric vector:



>str(df$Grad.Rate)
num [1:777] 60 56 54 59 15 55 63 73 80 52 ...


I want to update any element>100



> for (i in df$Grad.Rate){
+ if (i >100){
+ print(i)
+ i = 100
+ print(paste0('changed to ', i))
+ }
+ }
[1] 118
[1] "changed to 100"


After I run the for loop, the element that is >100 is still in the vector



> any(df$Grad.Rate>100)
[1] TRUE


Why?










share|improve this question

















This question already has an answer here:




  • Replace given value in vector

    5 answers




Element in a numeric vector does not change after running a for loop that iterates each element.



I have a numeric vector:



>str(df$Grad.Rate)
num [1:777] 60 56 54 59 15 55 63 73 80 52 ...


I want to update any element>100



> for (i in df$Grad.Rate){
+ if (i >100){
+ print(i)
+ i = 100
+ print(paste0('changed to ', i))
+ }
+ }
[1] 118
[1] "changed to 100"


After I run the for loop, the element that is >100 is still in the vector



> any(df$Grad.Rate>100)
[1] TRUE


Why?





This question already has an answer here:




  • Replace given value in vector

    5 answers








r for-loop vector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 14:50









Sven Hohenstein

65.6k12100131




65.6k12100131










asked Dec 31 '18 at 14:48









Kelvin ChenKelvin Chen

11




11




marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 16:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 16:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

    – akrun
    Dec 31 '18 at 14:49





















  • You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

    – akrun
    Dec 31 '18 at 14:49



















You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

– akrun
Dec 31 '18 at 14:49







You are just printing and not updating the dataset. You may need to loop through the sequence and update it. But, this can be done easily without a loop, df$Grad.Rate[df$Grad.Rate > 100] <- 100 or df$Grad_Rate <- pmax(100, df$Grad.Rate)

– akrun
Dec 31 '18 at 14:49














2 Answers
2






active

oldest

votes


















0














Instead of i = 100, you have to use



df$Grad.Rate[i] <- 100


in your loop. You can also choose to change the elements without a loop:



df$Grad.Rate[df$Grad.Rate > 100] <- 100





share|improve this answer































    0














    We can do this without any loop



    df$Grad.Rate[df$Grad.Rate > 100] <- 100


    Or



    df$Grad_Rate <- pmin(100, df$Grad.Rate) 




    In the for loop, the values are not updated. Instead, we can loop through the sequence and update it



    for (i in seq_along(df$Grad.Rate)){
    if (df$Grad.Rate[i] >100){

    df$Grad.Rate[i] <- 100

    }





    share|improve this answer
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Instead of i = 100, you have to use



      df$Grad.Rate[i] <- 100


      in your loop. You can also choose to change the elements without a loop:



      df$Grad.Rate[df$Grad.Rate > 100] <- 100





      share|improve this answer




























        0














        Instead of i = 100, you have to use



        df$Grad.Rate[i] <- 100


        in your loop. You can also choose to change the elements without a loop:



        df$Grad.Rate[df$Grad.Rate > 100] <- 100





        share|improve this answer


























          0












          0








          0







          Instead of i = 100, you have to use



          df$Grad.Rate[i] <- 100


          in your loop. You can also choose to change the elements without a loop:



          df$Grad.Rate[df$Grad.Rate > 100] <- 100





          share|improve this answer













          Instead of i = 100, you have to use



          df$Grad.Rate[i] <- 100


          in your loop. You can also choose to change the elements without a loop:



          df$Grad.Rate[df$Grad.Rate > 100] <- 100






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 14:52









          Sven HohensteinSven Hohenstein

          65.6k12100131




          65.6k12100131

























              0














              We can do this without any loop



              df$Grad.Rate[df$Grad.Rate > 100] <- 100


              Or



              df$Grad_Rate <- pmin(100, df$Grad.Rate) 




              In the for loop, the values are not updated. Instead, we can loop through the sequence and update it



              for (i in seq_along(df$Grad.Rate)){
              if (df$Grad.Rate[i] >100){

              df$Grad.Rate[i] <- 100

              }





              share|improve this answer






























                0














                We can do this without any loop



                df$Grad.Rate[df$Grad.Rate > 100] <- 100


                Or



                df$Grad_Rate <- pmin(100, df$Grad.Rate) 




                In the for loop, the values are not updated. Instead, we can loop through the sequence and update it



                for (i in seq_along(df$Grad.Rate)){
                if (df$Grad.Rate[i] >100){

                df$Grad.Rate[i] <- 100

                }





                share|improve this answer




























                  0












                  0








                  0







                  We can do this without any loop



                  df$Grad.Rate[df$Grad.Rate > 100] <- 100


                  Or



                  df$Grad_Rate <- pmin(100, df$Grad.Rate) 




                  In the for loop, the values are not updated. Instead, we can loop through the sequence and update it



                  for (i in seq_along(df$Grad.Rate)){
                  if (df$Grad.Rate[i] >100){

                  df$Grad.Rate[i] <- 100

                  }





                  share|improve this answer















                  We can do this without any loop



                  df$Grad.Rate[df$Grad.Rate > 100] <- 100


                  Or



                  df$Grad_Rate <- pmin(100, df$Grad.Rate) 




                  In the for loop, the values are not updated. Instead, we can loop through the sequence and update it



                  for (i in seq_along(df$Grad.Rate)){
                  if (df$Grad.Rate[i] >100){

                  df$Grad.Rate[i] <- 100

                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 31 '18 at 14:58

























                  answered Dec 31 '18 at 14:52









                  akrunakrun

                  407k13198272




                  407k13198272















                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'