Learn R Programming

gofastr (version 0.3.0)

sub_in_na: Regex Sub to Missing

Description

Use a regex to identify elements to sub out for missing NA. Useful within a magrittr pipeline before producing the TermDocumentMatrix or DocumentTermMatrix.

Usage

sub_in_na(x, regex = "^[^A-Za-z]*$", ...)

Arguments

x

A vector of text strings.

regex

A regex to match strings in a vector.

Other arguments passed to grepl

Value

Returns a vector with NAs inserted.

Examples

Run this code
# NOT RUN {
x <- c("45", "..", "", "   ", "dog")
sub_in_na(x)
sub_in_na(x, "^\\s*$")

# }
# NOT RUN {
library(tidyverse)
x %>%
    q_dtm() %>%
    as.matrix()

x %>%
    sub_in_na() %>%
    q_dtm() %>%
    as.matrix()
# }

Run the code above in your browser using DataLab