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.
"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.
We can use
library(stringr) sum(!!str_count(str1, letters)) # 3
Using the new dataset
i1 <- !sapply(df1$col1, function(x) any(str_count(x, letters)>1)) df1[i1,,drop=FALSE]
str1 <- "banana"
Answered By – akrun
Answer Checked By – David Goodson (BugsFixing Volunteer)