From 738f4b1ab5845ba2879f3d5fff2e60767ea691e5 Mon Sep 17 00:00:00 2001 From: gagolews Date: Sat, 21 Apr 2018 00:14:39 +0200 Subject: [PATCH] stri_na2empty --- NAMESPACE | 2 ++ NEWS | 2 ++ R/utils.R | 51 +++++++++++++++++++++++++++++++- man/stri_list2matrix.Rd | 4 +++ man/stri_na2empty.Rd | 25 ++++++++++++++++ man/stri_remove_empty.Rd | 31 +++++++++++++++++++ src/stri_encoding_conversion.cpp | 2 +- 7 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 man/stri_na2empty.Rd create mode 100644 man/stri_remove_empty.Rd diff --git a/NAMESPACE b/NAMESPACE index 57375cb66..2fc412760 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS b/NEWS index ad914caa2..7f3fb6f17 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/R/utils.R b/R/utils.R index 5d42f1af6..2a1c942d0 100644 --- a/R/utils.R +++ b/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 @@ -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)] +} diff --git a/man/stri_list2matrix.Rd b/man/stri_list2matrix.Rd index 6866569a8..3c7ee726a 100644 --- a/man/stri_list2matrix.Rd +++ b/man/stri_list2matrix.Rd @@ -55,3 +55,7 @@ stri_list2matrix(list("a", c("b", "c")), fill="") stri_list2matrix(list("a", c("b", "c")), fill="", n_min=5) } +\seealso{ +Other utils: \code{\link{stri_na2empty}}, + \code{\link{stri_remove_empty}} +} diff --git a/man/stri_na2empty.Rd b/man/stri_na2empty.Rd new file mode 100644 index 000000000..e3a2b75a4 --- /dev/null +++ b/man/stri_na2empty.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{stri_na2empty} +\alias{stri_na2empty} +\title{Replace NAs with empty strings} +\usage{ +stri_na2empty(x) +} +\arguments{ +\item{x}{a character vector} +} +\value{ +Always returns a character vector. +} +\description{ +This function replaces all missing values with empty strings +} +\examples{ +stri_na2empty(c("a", NA, "", "b")) + +} +\seealso{ +Other utils: \code{\link{stri_list2matrix}}, + \code{\link{stri_remove_empty}} +} diff --git a/man/stri_remove_empty.Rd b/man/stri_remove_empty.Rd new file mode 100644 index 000000000..ac7915277 --- /dev/null +++ b/man/stri_remove_empty.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{stri_remove_empty} +\alias{stri_remove_empty} +\title{Remove all empty strings from a character vector} +\usage{ +stri_remove_empty(x, na_empty = FALSE) +} +\arguments{ +\item{x}{a character vector} + +\item{na_empty}{should missing values be treated as empty strings?} +} +\value{ +Always returns a character vector. +} +\description{ +This function removes all empty strings from 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) + +} +\seealso{ +stri_isempty + +Other utils: \code{\link{stri_list2matrix}}, + \code{\link{stri_na2empty}} +} diff --git a/src/stri_encoding_conversion.cpp b/src/stri_encoding_conversion.cpp index a2a189e03..a4f842395 100644 --- a/src/stri_encoding_conversion.cpp +++ b/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