Home > front end >  tidy data by replacing "<LOD" with NA in multiple columns
tidy data by replacing "<LOD" with NA in multiple columns

Time:02-09

Ive got some scientific data that ive imported form a CSV to R

unfortuanly I have values that are "<LOD" like in the code below I need to change them to NA so that the coumbs can be numeric and I can start calculating averages etc.

i know mutate and

tb<-tibble(
      Mg=c(1,2,4,5,"<LOD"),
      Hg=c(5,10,15,"<LOD",11),
      Fe=c(15,"<LOD",10,4,23)
    )

I know something like this would work but I have 100 variables and I need to do this for all of them. Does anyone have any idea how I can apply it to all my columns quickly I tried Mutate_all but Str_replace needs the string to be defined.

tb%>%mutate(Mg=str_replace(Mg,string="<LOD", replacement = NA_character_))

thanks

CodePudding user response:

na_if can do this column wise on the full dataset

library(dplyr)
tb <- tb %>%
   na_if("<LOD") %>%
   type.convert(as.is = TRUE)

-output

tb
# A tibble: 5 × 3
     Mg    Hg    Fe
  <int> <int> <int>
1     1     5    15
2     2    10    NA
3     4    15    10
4     5    NA     4
5    NA    11    23

It may also be done by looping with across

tb <- tb %>%
      mutate(across(everything(), ~ as.numeric(na_if(.x, "<LOD"))))
  •  Tags:  
  • Related