Henri
Henri

Reputation: 1235

Use stringr and str_count to return the number of unique words in a string

Is there a way to use str_count to count unique words in a string? I would like the simple code below to return 2 instead of 6.

library(tidyverse)

string <- "Z AD Banana EW Z AD Z AD X" 

str_count(string, "Z|AD")

Returns: 6

Upvotes: 0

Views: 615

Answers (2)

akrun
akrun

Reputation: 886938

We can use

library(stringr)
library(purrr)
map_lgl(c("Z", "AD"), ~ str_detect(string, .x)) %>% sum
#[1] 2

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388797

One way would be to extract all the values that satisfy the pattern and then count unique values.

library(dplyr)
library(stringr)

n_distinct(str_extract_all(string, "Z|AD")[[1]])
#[1] 2

This can be written in base R as :

length(unique(regmatches(string, gregexpr("Z|AD", string))[[1]]))

Upvotes: 2

Related Questions