要查找字符串向量中唯一字符出现的次数,我们可以结合使用rle和strsplit的表函数。例如,如果我们有一个包含一些唯一且重复的值的字符串向量x,则可以使用以下命令创建它-
table(rle(strsplit(x,"")[[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次,依此类推。
> 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
> 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
> 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
> 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
> 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
> 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