本文介绍了R:计算该字符串中唯一字符的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,其中的一列是字符串类型。 我想计算该字符串中唯一/不同字符的数量。
I have a data frame 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再生产例子:
I有一个数据框,其中列是字符串类型。我需要过滤掉那些字符串只有一个不同字符的行。
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 纽约墨尔本
col1 col2 col3 new york melbourne
所以删除
推荐答案我们可以使用 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"更多推荐
R:计算该字符串中唯一字符的数量
发布评论