Skip to content

Commit

Permalink
stri_na2empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gagolews committed Apr 20, 2018
1 parent 0e48d0e commit 738f4b1
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -147,6 +147,7 @@ export(stri_match_first)
export(stri_match_first_regex)
export(stri_match_last)
export(stri_match_last_regex)
export(stri_na2empty)
export(stri_numbytes)
export(stri_opts_brkiter)
export(stri_opts_collator)
Expand All @@ -164,6 +165,7 @@ export(stri_rand_shuffle)
export(stri_rand_strings)
export(stri_read_lines)
export(stri_read_raw)
export(stri_remove_empty)
export(stri_replace)
export(stri_replace_all)
export(stri_replace_all_charclass)
Expand Down
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -15,6 +15,8 @@ when input was empty.

* [NEW FEATURE] #289: `stri_flatten` gained `na_empty` `omit_empty` arguments.

* [NEW FEATURE] New functions: `stri_remove_empty`, `stri_na2empty`

* [WARN] Removed `-Wparentheses` warnings in `icu55/common/cstring.h:38:63`
and `icu55/i18n/windtfmt.cpp` in the ICU4C 55.1 bundle.

Expand Down
51 changes: 50 additions & 1 deletion R/utils.R
@@ -1,5 +1,5 @@
## This file is part of the 'stringi' package for R.
## Copyright (c) 2013-2017, Marek Gagolewski and other contributors.
## Copyright (c) 2013-2018, Marek Gagolewski and other contributors.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -81,3 +81,52 @@
stri_list2matrix <- function(x, byrow=FALSE, fill=NA_character_, n_min=0) {
.Call(C_stri_list2matrix, x, byrow, stri_enc_toutf8(fill), n_min)
}


#' @title
#' Replace NAs with empty strings
#'
#' @description
#' This function replaces all missing values with empty strings
#'
#' @param x a character vector
#'
#' @return
#' Always returns a character vector.
#'
#' @examples
#' stri_na2empty(c("a", NA, "", "b"))
#'
#' @family utils
#' @export
stri_na2empty <- function(x) {
x <- stri_enc_toutf8(x)
x[is.na(x)] <- ""
x
}

#' @title
#' Remove all empty strings from a character vector
#'
#' @description
#' This function removes all empty strings from a character vector.
#'
#' @param x a character vector
#' @param na_empty should missing values be treated as empty strings?
#'
#' @return
#' Always returns a character vector.
#'
#' @examples
#' stri_remove_empty(stri_na2empty(c("a", NA, "", "b")))
#' stri_remove_empty(c("a", NA, "", "b"))
#' stri_remove_empty(c("a", NA, "", "b"), TRUE)
#'
#' @family utils
#' @export
stri_remove_empty <- function(x, na_empty=FALSE) {
x <- stri_enc_toutf8(x)
if (identical(na_empty, TRUE))
x[is.na(x)] <- ""
x[!stri_isempty(x)]
}
4 changes: 4 additions & 0 deletions man/stri_list2matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/stri_na2empty.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/stri_remove_empty.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/stri_encoding_conversion.cpp
@@ -1,5 +1,5 @@
/* This file is part of the 'stringi' package for R.
* Copyright (c) 2013-2017, Marek Gagolewski and other contributors.
* Copyright (c) 2013-2018, Marek Gagolewski and other contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down

0 comments on commit 738f4b1

Please sign in to comment.