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.