convert_blanks_to_na: Convert Blank Strings Into NAs

Description Usage Arguments Details Author(s) Examples

View source: R/utils.R

Description

Turn SAS blank strings into proper R NAs.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
convert_blanks_to_na(x)

## Default S3 method:
convert_blanks_to_na(x)

## S3 method for class 'character'
convert_blanks_to_na(x)

## S3 method for class 'list'
convert_blanks_to_na(x)

## S3 method for class 'data.frame'
convert_blanks_to_na(x)

Arguments

x

Any R object

Details

The default methods simply returns its input unchanged. The character method turns every instance of "" into NA_character_ while preserving all attributes. When given a data frame as input the function keeps all non-character columns as is and applies the just described logic to character columns. Once again all attributes such as labels are preserved.

Author(s)

Thomas Neitmann

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
convert_blanks_to_na(c("a", "b", "", "d", ""))

df <- tibble::tibble(
  a = structure(c("a", "b", "", "c"), label = "A"),
  b = structure(c(1, NA, 21, 9), label = "B"),
  c = structure(c(TRUE, FALSE, TRUE, TRUE), label = "C"),
  d = structure(c("", "", "s", "q"), label = "D")
)
print(df)
convert_blanks_to_na(df)

epijim/admiral documentation built on Feb. 13, 2022, 12:15 a.m.