Writing an If Statement to Eliminate Inequalities

I am working with a large dataset named "Master" that has lab data. Some of the parameters were analyzed as under the detection limit, for example "<0.5". I am trying to write n if statement that reassigns values with an inequality symbol as half of the value that follows it. All values are given as characters presently, and appear as NA when converted to numeric. There are only 4 detection limits, so each can be dealt with individually if that is easiest. I have tried the following:
gsub("<0.5","0.25",Master)
Master[Master=="<0.5"] <- "0.25"
I also tried using replace() with dplyr
Any help would be greatly appreciated.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

library(tidyverse)
library(readr)
master <- tibble(param_1 = c("<0.5", "<0.7", "<0.5", "1", "2"))

master2 <- master %>%
  mutate(processed_param1 = ifelse(str_detect(
    string = param_1,
    pattern = "<"
  ),
  parse_number(gsub(
    pattern = "<",
    replacement = "", x = param_1
  )) / 2, parse_number(param_1)
  ))
> master2
# A tibble: 5 x 2
param_1 processed_param1
<chr>              <dbl>
1 <0.5                0.25
2 <0.7                0.35
3 <0.5                0.25
4 1                   1   
5 2                   2

Thank you! I tried what you did, but I am getting an error message. I used the name of my column, Pheophytin, rather than "param_1".

Code:
library(tidyverse)
library(readr)

Master2 <- Master

mutate(processed_Pheophytin = ifelse(str_detect(
string=Pheophytin,
pattern="<"
),
parse_number(gsub(
pattern = "<",
replacement ="", x = Pheophytin
))/2, parse_number(Pheophytin)
))

Error Message:
Error in stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) : object 'Pheophytin' not found

You're missing the pipe %>% @nirgrahamuk has there -- if you include it, it should work.

Perfect, it works now! Thank you so much for the help!

If I name my new column Pheophytin instead of processed_Pheophytin will it replace the data rather than adding a new column? Or would it work better to reassign the new columns to replace the old ones in my original dataframe?

Master$Pheophytin <- Master2$processed_Pheophytin

Yes, you can learn how to use the tidyverse tools by reading this free book

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.