Home > Software design >  Is there a function to paste NA_character_?
Is there a function to paste NA_character_?

Time:01-14

As expected, the base R paste() function coerces NA_character_ to "NA".

From the documentation of the base R paste() function:

Note that paste() coerces NA_character_, the character missing value, to "NA" which may seem undesirable, e.g., when pasting two character vectors, or very desirable, e.g. in paste("the value of p is ", p).

example:

paste(NA_character_)

returns "NA"

Is there a function (magicFunction in the example below) that preserves NA_character_? i.e. that

magicFunction(NA_character_)

would return NA and not "NA"?

I woudld like to use the function in the following context:

library(tidyverse)
toColumn = list(df1 = tibble(letts = c("a","b",NA_character_,"c")),
                df2 = tibble(letts = c("alpha","beta",NA_character_,"gamma")),
                df3 = tibble(letts = c(NA_character_)))
                
final_df  = toColumn %>% purrr::map(~.x %>% dplyr::pull(letts) %>%
  paste(.,collapse=";") %>% dplyr::tibble(pasted_letts = .) ) %>% 
  dplyr::bind_rows(.)

where the third row of the final data frame column "pasted_letts" containsNA and I would like to preserve the NA_character_ of the original data frame df3 in order to use e.g. is.na() for filtering, mutate etc..

CodePudding user response:

You could make your own:

paste_keep_na = function (..., sep = ' ', collapse = NULL, recycle0 = FALSE) {
    if (...length() == 1L && length(..1) == 1L && is.na(..1)) {
        return(..1)
    }
    paste(..., sep = sep, collapse = collapse, recycle0 = recycle0)
}

Note that this will only test the first argument. If you pass multiple values (or a single value with multiple elements), the function will behave like regular paste, which is probably what’s expected.

CodePudding user response:

Do you mean something like this?

> deparse(NA_character_)
[1] "NA_character_"
  •  Tags:  
  • Related