Home > Enterprise >  Replacing text in column with column-specific value
Replacing text in column with column-specific value

Time:08-16

I want to replace "Out of range" text with a minimum value found from each column and subtract 0.01 from that. How could I create a loop to do this without having to go through a large dataset manually?

enter image description here

for example here, replace OOR < with 0.18

CodePudding user response:

One option is using case_when function and replace the specific value. Here a example:

library(dplyr)
data %>% 
mutate(
   text_colum = case_when("OOR <" ~ min(text_colum) - 0.01, 
                        TRUE ~ text_colum)
)

CodePudding user response:

One option would be gsub.

data <- data.frame("Value" = c(0.75, 0.19, 2.25999999, "OOR <"))
    data
       Value
1       0.75
2       0.19
3 2.25999999
4       OOR <

data$Value <- gsub("OOR <", "0.18", data$Value)
data
       Value
1       0.75
2       0.19
3 2.25999999
4       0.18

However, you could use str_replace too (from stringr package).

library(stringr)
data$Value <- str_replace(data$Value, "OOR <", "0.18")
  •  Tags:  
  • r
  • Related