covert -Inf to 0 r
# Step 1: Create a vector with values including -Inf
vec <- c(1, 2, -Inf, 4, 5)
# Step 2: Use the ifelse() function to replace -Inf with 0
vec_fixed <- ifelse(vec == -Inf, 0, vec)
Explanation:
Step 1: We create a vector called vec
that contains some values, including -Inf
(negative infinity). The vector vec
is initialized with the values 1, 2, -Inf, 4, 5
.
Step 2: We use the ifelse()
function to replace any occurrence of -Inf
in the vector vec
with 0
. The ifelse()
function takes three arguments: the condition to check (vec == -Inf
), the value to return if the condition is TRUE
(0
), and the value to return if the condition is FALSE
(the original value from vec
). The result is stored in the vector vec_fixed
.
So, after executing these steps, the vector vec_fixed
will have the values 1, 2, 0, 4, 5
, where -Inf
has been replaced with 0
.