Home > OS >  Perform a for() loop to replace those elements of vec_tor that are greater than "5" with t
Perform a for() loop to replace those elements of vec_tor that are greater than "5" with t

Time:09-23

Perform a for() loop to replace those elements of vec_tor that are greater than "5" with the number "5". #can use the functions for() and seq_along()

CodePudding user response:

It doesn't need a loop though

vec_tor[vec_tor > 5] <- 5

Or in replace

replace(vec_tor, vec_tor > 5, 5)

Or with pmin

pmin(vec_tor, 5)

In a for loop, it would be

for(i in seq_along(vec_tor)) {
    if(vec_tor[i] > 5) {
        vec_tor[i] <- 5
     }
}
  •  Tags:  
  • r
  • Related