Why for loop does not change element in a numeric vector? [duplicate]
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?
r for-loop vector
marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas
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.
add a comment |
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?
r for-loop vector
marked as duplicate by markus, phiver, IceCreamToucan, Rui Barradas
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 justprinting 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] <- 100ordf$Grad_Rate <- pmax(100, df$Grad.Rate)
– akrun
Dec 31 '18 at 14:49
add a comment |
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?
r for-loop vector
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
r for-loop vector
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
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
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 justprinting 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] <- 100ordf$Grad_Rate <- pmax(100, df$Grad.Rate)
– akrun
Dec 31 '18 at 14:49
add a comment |
You are justprinting 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] <- 100ordf$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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Dec 31 '18 at 14:52
Sven HohensteinSven Hohenstein
65.6k12100131
65.6k12100131
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
edited Dec 31 '18 at 14:58
answered Dec 31 '18 at 14:52
akrunakrun
407k13198272
407k13198272
add a comment |
add a comment |
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] <- 100ordf$Grad_Rate <- pmax(100, df$Grad.Rate)– akrun
Dec 31 '18 at 14:49