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

R:计算该字符串中唯一字符的数量,r,string,count,R,String,Count,我有一个数据框,其中一列的类型是string。 我想计算该字符串中唯一/不同字符的数量 例如 一个例子: 我有一个数据框,其中一列是string类型。我需要过滤掉那些字符串只有一个不同字符的行 col1 col2 col3 new york qqqq melbourne aaaaaa 我需要一个最终的数据帧,如 col1 col2 col3 纽约 墨尔本 因此,请完全删除这些行。我们可以使用stru count library(stringr) sum(!!str

我有一个数据框,其中一列的类型是string。 我想计算该字符串中唯一/不同字符的数量

例如

一个例子:

我有一个数据框,其中一列是string类型。我需要过滤掉那些字符串只有一个不同字符的行

col1         col2       col3 
new york
qqqq
melbourne
aaaaaa
我需要一个最终的数据帧,如

col1 col2 col3 纽约 墨尔本


因此,请完全删除这些行。

我们可以使用
stru count

library(stringr)
sum(!!str_count(str1, letters))
#[1] 3
更新 使用新数据集

i1 <- !sapply(df1$col1, function(x) any(str_count(x, letters)>1))
df1[i1,,drop=FALSE]
i1)
df1[i1,,drop=FALSE]
数据
str1这不假设“字符”为
字母
,并避免生成R数据结构:

library(inline)

.char_unique_code <- "
std::vector < std::string > s = as< std::vector < std::string > >(x);
unsigned int input_size = s.size();

std::vector < std::string > chrs(input_size);

for (unsigned int i=0; i<input_size; i++) {

  std::string t = s[i];

  for (std::string::iterator chr=t.begin();
       chr != t.end(); ++chr) {

    if (chrs[i].find(*chr) == std::string::npos) {
      chrs[i] += *chr;
    }

  }

}
return(wrap(chrs));
"

char_unique <- 
  rcpp(sig=signature(x="std::vector < std::string >"),
       body=.char_unique_code,
       includes=c("#include <string>",
                 "#include <iostream>"))

nchar(char_unique("banana"))
## [1] 3
库(内联)

.char_unique_code
length(unique(strsplit(“香蕉”),“”)[[1]])
在新的数据集中,
melbourne
确实有2个
e
,但我不想删除任何重复字母的单词。我想删除所有字母都相同的单词。原因是:有很多人为了避免给出这些数据,在我的系统中只放了几个字符,如“aaaaa”,我想将其标记为无效。“字符”不一定是字母,但至少在OP显示的示例中是字母。谢谢你的回答。当我申请时data@DroppingOff实际上,
sum(…)
部分只是字符串中唯一字符的总和。你能用一个可复制的例子更新你的帖子吗?现在添加了一个例子谁突然删除了重复标签?这是不同的问题。我认为链接是由@Cath提供的,所以应该是重复的。你没有固定
长度(唯一(strsplit(str1,“,fixed=TRUE)[[1]])
。而且,在我看来,在一个6个字母的单词上坐板凳有点奇怪。所有这些都在进行中。我想先把C++版本矢量化。敬请期待。
str1 <- "banana"
library(inline)

.char_unique_code <- "
std::vector < std::string > s = as< std::vector < std::string > >(x);
unsigned int input_size = s.size();

std::vector < std::string > chrs(input_size);

for (unsigned int i=0; i<input_size; i++) {

  std::string t = s[i];

  for (std::string::iterator chr=t.begin();
       chr != t.end(); ++chr) {

    if (chrs[i].find(*chr) == std::string::npos) {
      chrs[i] += *chr;
    }

  }

}
return(wrap(chrs));
"

char_unique <- 
  rcpp(sig=signature(x="std::vector < std::string >"),
       body=.char_unique_code,
       includes=c("#include <string>",
                 "#include <iostream>"))

nchar(char_unique("banana"))
## [1] 3
library(stringr)
library(microbenchmark)
library(ggplot2)

str_char_ct_unique <- function(x) sum(!!str_count(x, letters))
char_ct_unique <- function(x) nchar(char_unique(x))
r_char_ct_unique <- function(x) length(unique(strsplit(x, "")[[1]]))

microbenchmark(stringr=str_char_ct_unique("banana"),
                  rcpp=char_ct_unique("banana"),
                     r=r_char_ct_unique("banana"),
               times=1000) -> mb

## Unit: microseconds
##     expr     min       lq       mean   median       uq     max neval cld
##  stringr 125.978 129.1765 139.271061 130.9415 139.3870 334.563  1000   c
##     rcpp   1.458   2.0160   3.002184   2.6345   3.1365  32.244  1000 a  
##        r   4.797   6.1070   8.292847   7.3380   8.0505  86.709  1000  b 
library(random)
library(purrr)

char_ct_unique <- function(x) nchar(char_unique(x))
r_char_ct_unique <- function(x) map_int(map(x, function(x) unique(strsplit(x, "")[[1]])), length)

tst <- as.vector(randomStrings(n=100, len=20, unique=FALSE))

sum(char_ct_unique(tst) == r_char_ct_unique(tst))
## [1] 100

microbenchmark(rcpp=char_ct_unique(tst),
                  r=r_char_ct_unique(tst),
               times=1000) 

## Unit: microseconds
##  expr     min       lq      mean   median      uq      max neval cld
##  rcpp  53.643  56.2375  66.69311  60.2740  68.178  250.992  1000  a 
##     r 683.420 759.4070 952.14407 822.8905 922.710 6513.508  1000   b
dat <- readLines("https://gist.githubusercontent.com/hrbrmstr/f80b157b383134b37fb3/raw/534b4c79e7c51710c6db6961bc5dc5ec25c4242b/gistfile1.txt")
digest::digest(dat, "sha1", serialize=FALSE)
## [1] "6c6695dd2f314762c81e6e6891ec1c138a4f3a08"

nchar(dat)
## [1] 10000

char_ct_unique(dat) == r_char_ct_unique(dat)
## [1] TRUE

microbenchmark(rcpp=char_ct_unique(dat),
                  r=r_char_ct_unique(dat),
               times=1000)

## Unit: microseconds
##  expr     min      lq     mean  median      uq      max neval cld
##  rcpp  73.801 110.681 122.9091 118.330 139.373  308.602  1000  a 
##     r 377.556 430.703 533.9120 448.631 492.466 4275.568  1000   b
f_r_char_ct_unique <- function(x) map_int(map(x, function(x) unique(strsplit(x, "", fixed=TRUE)[[1]])), length)
dat <- c(dat, toupper(dat), tolower(dat))

microbenchmark(rcpp=char_ct_unique(dat),
                  r=r_char_ct_unique(dat),
                 fr=f_r_char_ct_unique(dat),
               times=1000) 

## Unit: microseconds
##  expr      min       lq     mean   median        uq       max neval
##  rcpp  218.299  284.143  331.319  332.281  358.1215   696.907  1000
##     r 1266.976 1442.460 1720.320 1494.167 1634.7870  5896.685  1000
##    fr 1260.027 1444.298 1769.664 1501.416 1652.8895 78457.729  1000