Replace "ND" (Non Detect) values in data frame to zero

I am attempting to write code to find all "ND" values in a large data frame named "Master" and reassign them as 0. I tried this:

library(tidyverse)
library(readr)
#VSS

Master1 <- Master %>%
  
  mutate(VSS = ifelse(str_detect(
         string=VSS,
         pattern="ND"
         ),
         parse_number(gsub(
           pattern = "ND",
           replacement ="", x = 0, parse_number(VSS)
  ))))

but I wind up with this error message:

# Error in ifelse(str_detect(string = VSS, pattern = "ND"), parse_number(gsub(pattern = "ND", : argument "no" is missing, with no default

If there is a better method to search all columns in the dataframe at once and replace them, that would be preferred.

Data Example: (Master)
TDS TSS VSS TKN DOC TOC DON Chla
10 2.2 3.5 ND 10 2.2 3.5 ND
14 2.5 3.75 0.01 14 2.5 3.75 0.01
ND N/A 5 0.2 ND N/A 5 0.2
10 2.2 3.5 ND 10 2.2 3.5 ND
14 2.5 3.75 0.01 14 2.5 3.75 0.01
ND N/A 5 0.2 ND N/A 5 0.2
14 2.5 3.75 0.01 14 2.5 3.75 0.01
ND N/A 5 0.2 ND N/A 5 0.2
10 2.2 3.5 ND 10 2.2 3.5 ND
14 2.5 3.75 0.01 14 2.5 3.75 0.01
ND N/A 5 0.2 ND N/A 5 0.2

*Please note that I want to maintain N/As, which I will change to blanks later

Is this what you wan to do?

library(dplyr)

master <- data.frame(
  stringsAsFactors = FALSE,
               TDS = c("10","14","ND","10","14",
                       "ND","14","ND","10","14","ND"),
               TSS = c("2.2","2.5","N/A","2.2",
                       "2.5","N/A","2.5","N/A","2.2","2.5","N/A"),
               VSS = c(3.5, 3.75, 5, 3.5, 3.75, 5, 3.75, 5, 3.5, 3.75, 5),
               TKN = c("ND","0.01","0.2","ND",
                       "0.01","0.2","0.01","0.2","ND","0.01","0.2"),
               DOC = c("10","14","ND","10","14",
                       "ND","14","ND","10","14","ND"),
               TOC = c("2.2","2.5","N/A","2.2",
                       "2.5","N/A","2.5","N/A","2.2","2.5","N/A"),
               DON = c(3.5, 3.75, 5, 3.5, 3.75, 5, 3.75, 5, 3.5, 3.75, 5),
              Chla = c("ND","0.01","0.2","ND",
                       "0.01","0.2","0.01","0.2","ND","0.01","0.2")
)

master %>% 
    mutate_if(is.character, ~ if_else(. == "ND", "0", .)) %>% 
    mutate_all(as.numeric) # Your variables are saved as text, so this extra step would be a good idea

#>    TDS TSS  VSS  TKN DOC TOC  DON Chla
#> 1   10 2.2 3.50 0.00  10 2.2 3.50 0.00
#> 2   14 2.5 3.75 0.01  14 2.5 3.75 0.01
#> 3    0  NA 5.00 0.20   0  NA 5.00 0.20
#> 4   10 2.2 3.50 0.00  10 2.2 3.50 0.00
#> 5   14 2.5 3.75 0.01  14 2.5 3.75 0.01
#> 6    0  NA 5.00 0.20   0  NA 5.00 0.20
#> 7   14 2.5 3.75 0.01  14 2.5 3.75 0.01
#> 8    0  NA 5.00 0.20   0  NA 5.00 0.20
#> 9   10 2.2 3.50 0.00  10 2.2 3.50 0.00
#> 10  14 2.5 3.75 0.01  14 2.5 3.75 0.01
#> 11   0  NA 5.00 0.20   0  NA 5.00 0.20

Created on 2020-03-19 by the reprex package (v0.3.0.9001)

Have in mind that NA (Not Available) is the way R handles the "blanks", if you replace that for let's say this " ", you are going to get a character variable and you are not going to be able to use it for numerical calculations.

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

Yes, thank you! I appreciate the heads up on the NAs too, I will leave them as is

I have actually run into a problem here. This code only works if I change everything to numeric (mutate_all(as.numeric)), although I have to run the code through a filter that eliminates inequalities by replacing them with half of their value. If I filter the code first, NAs are created by coercion and NDs are lost. If I run this code first, the inequalities are lost when I change everything to numeric. I believe you helped me with my filter code as well, but I will include it for reference:

library(tidyverse)
library(readr)
#Pheophytin

Master2 <- Master %>%

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

Never mind, I fixed it!