如何找到R中字符串向量中唯一字符和重复字符的出现次数?
要查找字符串向量中唯一字符出现的次数,我们可以结合使用rle和strsplit的表函数。例如,如果我们有一个包含一些唯一且重复的值的字符串向量x,则可以使用以下命令创建它-
table(rle(strsplit(x,"")[[1]]))
例子1
> x1<-c("ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD") > x1输出结果
[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD"
示例
> table(rle(strsplit(x1,"")[[1]]))输出结果
values lengths A B C D F J K S V 1 5 1 3 6 3 7 4 3 1 2 0 0 1 1 0 0 0 0 0
这意味着长度1的A发生5次,长度2的A发生0次,依此类推。
例子2
> x2<-c("AAABDDDDDNDNDND") > table(rle(strsplit(x2,"")[[1]]))输出结果
values lengths A B D N 1 0 1 3 3 3 1 0 0 0 5 0 0 1 0
例子3
> x3<-c("ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKDDJDAJADJKFDSJFDADDKLOLIIYTYRYWALEODODADFNVLKVADKSFDFOPGDOSPAFSDGANJSDEWAYEKALDLAFPPOIIUYQTQRWTWJEMCNBZBCVDGDKALAQPORUWY") > x3输出结果
[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKDDJDAJADJKFDSJFDADDKLOLIIYTYRYWALEODODADFNVLKVADKSFDFOPGDOSPAFSDGANJSDEWAYEKALDLAFPPOIIUYQTQRWTWJEMCNBZBCVDGDKALAQPORUWY"
示例
> table(rle(strsplit(x3,"")[[1]]))输出结果
values lengths A B C D E F G I J K L M N O P Q R S T U V W Y Z 1 18 3 5 20 4 10 3 0 13 10 7 1 3 7 3 3 3 8 3 2 4 5 6 1 2 0 0 1 3 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
例子4
> x4<-c("oijgfhdkadkdslldsldslnvfdflllldllyrhhwjwjenddkdkdkdjjckkfkfjghjfkdkdaadjjjdkdkakdncnjdjggjdhjeanmwueueuehhdjskskdkd") > x4输出结果
[1] "oijgfhdkadkdslldsldslnvfdflllldllyrhhwjwjenddkdkdkdjjckkfkfjghjfkdkdaadjjjdkdkakdncnjdjggjdhjeanmwueueuehhdjskskdkd"
示例
> table(rle(strsplit(x4,"")[[1]]))输出结果
values lengths a c d e f g h i j k l m n o r s u v w y 1 3 2 21 5 6 2 3 1 10 14 2 1 5 1 1 5 3 1 3 1 2 1 0 1 0 0 1 2 0 1 1 2 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
例子5
> x5<-c("dajdsallkdafkdsfjdfsjdaskdskldsahdhdhdhdddddyrrteyeussldlddkcjchccccccchdsfjdsjjeuedhjdkaldldl") > x5输出结果
[1] "dajdsallkdafkdsfjdfsjdaskdskldsahdhdhdhdddddyrrteyeussldlddkcjchccccccchdsfjdsjjeuedhjdkaldldl"
示例
> table(rle(strsplit(x5,"")[[1]]))输出结果
values lengths a c d e f h j k l r s t u y 1 6 2 18 4 4 7 6 6 6 0 8 1 2 2 2 0 0 1 0 0 0 1 0 1 1 1 0 0 0 5 0 0 1 0 0 0 0 0 0 0 0 0 0 0 7 0 1 0 0 0 0 0 0 0 0 0 0 0 0
例子6
> x6<-c("hryrhhdjdakadldldlaldaldldlldddddlkdfjdjjdkdkeepaotirueuwkslsdesssldldkeie") > x6输出结果
[1] "hryrhhdjdakadldldlaldaldldlldddddlkdfjdjjdkdkeepaotirueuwkslsdesssldldkeie"
示例
> table(rle(strsplit(x6,"")[[1]]))输出结果
values lengths a d e f h i j k l o p r s t u w y 1 5 15 4 1 1 2 2 6 10 1 1 3 2 1 2 1 1 2 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
例子7
> x7<-c("qqooqooeoeorrppppptptoorororofflglgllhjjjjhkglllflllfsoososananbbbvccbbcbcbbcmmmawwwbbbzzzswwqqllooipp") > x7输出结果
[1] "qqooqooeoeorrppppptptoorororofflglgllhjjjjhkglllflllfsoososananbbbvccbbcbcbbcmmmawwwbbbzzzswwqqllooipp"
示例
> table(rle(strsplit(x7,"")[[1]]))输出结果
values lengths a b c e f g h i j k l m n o p q r s t v w z 1 3 1 3 2 2 3 2 1 0 1 2 0 2 6 1 1 3 4 2 1 0 0 2 0 2 1 0 1 0 0 0 0 0 2 0 0 5 1 2 1 0 0 0 1 0 3 0 2 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 1 1 4 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
例子八
> x8<-c("EHEJAIDKDLLDLFAYWYWIWOLWLLLSLSLLSLSLSLSSLSPPPOOUTUJWWNNNSSNNANNDMQQLLLJJEPEPEPPPRPRPSPSPS") > x8输出结果
[1] "EHEJAIDKDLLDLFAYWYWIWOLWLLLSLSLLSLSLSLSSLSPPPOOUTUJWWNNNSSNNANNDMQQLLLJJEPEPEPPPRPRPSPSPS"
示例
> table(rle(strsplit(x8,"")[[1]]))输出结果
values lengths A D E F H I J K L M N O P Q R S T U W Y 1 3 4 5 1 1 2 2 1 7 1 0 1 6 0 2 9 1 2 4 2 2 0 0 0 0 0 0 1 0 2 0 2 1 0 1 0 2 0 0 1 0 3 0 0 0 0 0 0 0 0 2 0 1 0 2 0 0 0 0 0 0 0