[SOLVED] Count the number of unique characters in a string

Table of Contents

Issue

I have a dataframe where one of the columns is of type string.
I would like to count the number of unique/distinct characters in that string.

eg.

"banana" -> 3
'he' -> 2

A reproducible example:

I have a data frame where a column is type string. I would need to filter out those rows where the string has only one distinct character.

col1         col2       col3 
new york
qqqq
melbourne
aaaaaa

I would need to have a final data frame like

col1      col2    col3 
new york
melbourne

So delete those rows completely.

Solution

We can use str_count

library(stringr)
sum(!!str_count(str1, letters))
#[1] 3

Update

Using the new dataset

i1 <- !sapply(df1$col1, function(x) any(str_count(x, letters)>1))
df1[i1,,drop=FALSE]

data

str1 <- "banana"

Answered By – akrun

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *