Access ND values within parenthesis ND(xx) in data frame

I have a data set with environmental data with some non-detect (ND) values. These are written as ND(xx). I would like to either divide the (xx) by 2, or set it to zero and ultimately get rid of the ND() part so that I can use it as a numerical value for my analysis.

Here's an example of what my data looks like:


I would like to use the number within the parenthesis in my analysis, but I don't know how to access it. Is there a way to do it?


You can use some regular expressions to extract only the values


myData = data.frame(
  val = c("0,096", "ND(0,15)", "0,11")

myData$val = str_extract(myData$val, "\\d+,\\d+")
#>     val
#> 1 0,096
#> 2  0,15
#> 3  0,11

Created on 2022-06-02 by the reprex package (v2.0.1)

In regular expressions, \d+ means match one or more digits. The \ character needs to be escaped in an R string, so you write it as \\d+

Also, if these values are numbers in comma format (instead of period as R expects) you can use some more regular expressions to convert this as well

myData$val = as.numeric(str_replace(myData$val, ",", "\\."))

Hope this helps,

1 Like

It really helped! Thank you!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.