Skip to content

unite(na.rm = TRUE) only removes character NAs #765

@davidhunterwalsh

Description

@davidhunterwalsh

The NA values from all-NA columns get concatenated in as "NA" strings, as if na.rm = FALSE. If this is intended, then that's not documented.

library(tidyverse)

data <- tribble(
  ~Name,    ~Postalcode, ~Parent,  ~Parent2, ~Parent3, 
  "Paul",   "4732",      "Mother", NA,       NA,       
  "Edward", "9045",      NA,       NA,       NA, 
  "Mary",   "3476",      "Mother", NA,       NA,       
  NA,       NA,          NA,       NA,       NA,      
  NA,       "2468",      NA,       NA,       NA
)

# The NAs from both Parent2 and Parent3 are pasted in as strings, while the NAs
# from Parent1 are properly removed
data %>% unite(Parent_full, Parent:Parent3, sep = "|", na.rm = TRUE)
#> # A tibble: 5 x 3
#>   Name   Postalcode Parent_full 
#>   <chr>  <chr>      <chr>       
#> 1 Paul   4732       Mother|NA|NA
#> 2 Edward 9045       NA|NA       
#> 3 Mary   3476       Mother|NA|NA
#> 4 <NA>   <NA>       NA|NA       
#> 5 <NA>   2468       NA|NA

# Add a value anywhere in Parent3, and all its NAs get removed, but Parent2 is
# still getting pasted in in the middle
data[[2, "Parent3"]] <- "Uncle"
data %>% unite(Parent_full, Parent:Parent3, sep = "|", na.rm = TRUE)
#> # A tibble: 5 x 3
#>   Name   Postalcode Parent_full
#>   <chr>  <chr>      <chr>      
#> 1 Paul   4732       Mother|NA  
#> 2 Edward 9045       NA|Uncle   
#> 3 Mary   3476       Mother|NA  
#> 4 <NA>   <NA>       NA         
#> 5 <NA>   2468       NA

# Add a value to Parent3, and now there's no columns with all NAs, so no NAs are
# pasted in (also, concatenating all-missing values results in "" instead of an NA)
data[[1, "Parent2"]] <- "Aunt"
data %>% unite(Parent_full, Parent:Parent3, sep = "|", na.rm = TRUE)
#> # A tibble: 5 x 3
#>   Name   Postalcode Parent_full
#>   <chr>  <chr>      <chr>      
#> 1 Paul   4732       Mother|Aunt
#> 2 Edward 9045       Uncle      
#> 3 Mary   3476       Mother     
#> 4 <NA>   <NA>       ""         
#> 5 <NA>   2468       ""

Created on 2019-09-28 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorstrings 🎻

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions