View source: R/replace-with-nas.R
replace_with_nas | R Documentation |
NA
s for a variety of data types.Elements of zero-length are converted to NA
s. Can force
coercion to an optionally-specified data type.
The function has two parts.
First, it uses dplyr::na_if(x, "")
.
Second, it (optionally) coerces to the desired data type.
replace_with_nas(x, return_type = NULL)
x |
An array of values. It is temporarily coerced to a string. Required |
return_type |
Data type of returned vector. Optional |
If return_type
is missing, returned data type will match input.
Supports coercion to integer
, numeric
, character
, logical
,
and Date
vectors.
If return_type=logical
, a logical
vector will be returned
if x
contains only blanks and the characters "0"
and "1"
.
An array of values with NA
s.
Contact the package author if you'd like the function generalized so that additional values
(other that ""
) are converted to NA
s.
Will Beasley
library(OuhscMunge) #Load the package into the current R session.
replace_with_nas(c("a", "b", "", "d", ""))
replace_with_nas(c("a", "b", "", "d", ""), return_type="character")
replace_with_nas(c(1, 2, "", "", 5), return_type="character")
replace_with_nas(c(1, 2, "", "", 5)) #Equivalent to previous line.
replace_with_nas(c(1, 2, "", "", 5), return_type="integer")
replace_with_nas(c(1, 2, "", "", 5), return_type="numeric")
replace_with_nas(c("2011-02-03", "", "", "2011-02-24"), return_type="Date")
replace_with_nas(c("T", "", "", "F", "FALSE", "", "TRUE"), return_type="logical")
replace_with_nas(c("1", "", "", "0", "0" , "", "1") , return_type="logical")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.