R:计算该字符串中唯一字符的数量

编程入门 行业动态 更新时间:2024-10-17 23:26:31
本文介绍了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:计算该字符串中唯一字符的数量

本文发布于:2023-11-29 12:36:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   数量   中唯一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!