Home > database >  multiple str_replace functions within the same mutate()
multiple str_replace functions within the same mutate()

Time:12-05

my dummy code:

x <- c("A", "B", "C", "D")
y <- c("<0.5", "~1", "<10", "~30")

df <- data.frame(x,y) %>%
  mutate(y1 = str_replace(y, "~", ""),
         y2 = as.numeric(str_replace(y1, "<", ""))/2)

Basically what I want to do in column y is:

  • Remove the "~" from values that contain "~"
  • Remove "<" from values that contain "<", then halve those values

Ideally I'll come out with a fully numeric column.

How do I go about this step without needing the interim "y1" variable? I've tried putting both into str_replace but doesn't seem to work, or creates NAs. I've also tried piping within the str_replace but that doesn't work either. Note I only want the "<" values halved.

Thanks.

CodePudding user response:

We can concatenate several remove conditions by the OR operator:

library(tidyverse)

df <- data.frame(x = c("A", "B", "C", "D"),
                 y = c("<0.5", "~1", "<10", "~30"))

df %>%
  mutate(y2 = as.numeric(str_remove(y, "<|~")),
         y2 = if_else(str_detect(y, '<'), 0.5 * y2, y2))

which gives:

  x    y    y2
1 A <0.5  0.25
2 B   ~1  1.00
3 C  <10  5.00
4 D  ~30 30.00
  • Related