Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make elements NA depending on a predicate function

Tags:

r

na

How can I easily change elements of a list or vectors to NAs depending on a predicate ?

I need it to be done in a single call for smooth integration in dplyr::mutate calls etc...

expected output:

make_na(1:10,`>`,5)
# [1]  1  2  3  4  5 NA NA NA NA NA

my_list <- list(1,"a",NULL,character(0))    
make_na(my_list, is.null)
# [[1]]
# [1] 1
# 
# [[2]]
# [1] "a"
# 
# [[3]]
# [1] NA
# 
# [[4]]
# character(0)

Note:

I answered my question as I have one solution figured out but Id be happy to get alternate solutions. Also maybe this functionality is already there in base R or packaged in a prominent library

Was inspired by my frustration in my answer to this post

like image 500
Moody_Mudskipper Avatar asked Sep 19 '26 10:09

Moody_Mudskipper


1 Answers

We can build the following function:

make_na <- function(.x,.predicate,...) {
  is.na(.x) <- sapply(.x,.predicate,...)
  .x
}

Or a bit better to leverage purrr's magic :

make_na <- function(.x,.predicate,...) {
  if (requireNamespace("purrr", quietly = TRUE)) {
    is.na(.x) <- purrr::map_lgl(.x,.predicate,...)
  } else {
    if("formula" %in% class(.predicate))
      stop("Formulas aren't supported unless package 'purrr' is installed") 
    is.na(.x) <- sapply(.x,.predicate,...)
  }
  .x
}

This way we'll be using purrr::map_lgl if library purrr is available, sapply otherwise.

Some examples :

make_na <- function(.x,.predicate,...) {
  is.na(.x) <- purrr::map_lgl(.x,.predicate,...)
  .x
}

Some use cases:

make_na(1:10,`>`,5)
# [1]  1  2  3  4  5 NA NA NA NA NA

my_list <- list(1,"a",NULL,character(0))
make_na(my_list, is.null)
# [[1]]
# [1] 1
# 
# [[2]]
# [1] "a"
# 
# [[3]]
# [1] NA
# 
# [[4]]
# character(0)

make_na(my_list, function(x) length(x)==0)
# [[1]]
# [1] 1
# 
# [[2]]
# [1] "a"
# 
# [[3]]
# [1] NA
# 
# [[4]]
# [1] NA

If purrr is installed we can use this short form:

make_na(my_list, ~length(.x)==0)
like image 124
Moody_Mudskipper Avatar answered Sep 22 '26 08:09

Moody_Mudskipper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!