# How to find the number of occurrences of unique and repeated characters in a string vector in R?

To find the number of occurrences of unique characters in a string vector, we can use table function with the combination of rle and strsplit. For example, if we have a string vector x that contains some unique and repeated values then it can be created by using the below command −

table(rle(strsplit(x,"")[[1]]))

## Example 1

Live Demo

> x1<-c("ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD")
> x1

## Output

[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD"

## Example

> table(rle(strsplit(x1,"")[[1]]))

## Output

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

It means A of length 1 occurred 5 times and A of length 2 occurred 0 times and so on.

## Example 2

Live Demo

> x2<-c("AAABDDDDDNDNDND")
> table(rle(strsplit(x2,"")[[1]]))

## Output

values
lengths A B D N
1 0 1 3 3
3 1 0 0 0
5 0 0 1 0

## Example 3

Live Demo

> x3<-c("ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKDDJDAJADJKFDSJFDADDKLOLIIYTYRYWALEODODADFNVLKVADKSFDFOPGDOSPAFSDGANJSDEWAYEKALDLAFPPOIIUYQTQRWTWJEMCNBZBCVDGDKALAQPORUWY")
> x3

## Output

[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKDDJDAJADJKFDSJFDADDKLOLIIYTYRYWALEODODADFNVLKVADKSFDFOPGDOSPAFSDGANJSDEWAYEKALDLAFPPOIIUYQTQRWTWJEMCNBZBCVDGDKALAQPORUWY"

## Example

> table(rle(strsplit(x3,"")[[1]]))

## Output

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

## Example 4

Live Demo

> x4<-c("oijgfhdkadkdslldsldslnvfdflllldllyrhhwjwjenddkdkdkdjjckkfkfjghjfkdkdaadjjjdkdkakdncnjdjggjdhjeanmwueueuehhdjskskdkd")
> x4

## Output

[1] "oijgfhdkadkdslldsldslnvfdflllldllyrhhwjwjenddkdkdkdjjckkfkfjghjfkdkdaadjjjdkdkakdncnjdjggjdhjeanmwueueuehhdjskskdkd"

## Example

> table(rle(strsplit(x4,"")[[1]]))

## Output

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

## Example 5

Live Demo

> x5<-c("dajdsallkdafkdsfjdfsjdaskdskldsahdhdhdhdddddyrrteyeussldlddkcjchccccccchdsfjdsjjeuedhjdkaldldl")
> x5

## Output

[1] "dajdsallkdafkdsfjdfsjdaskdskldsahdhdhdhdddddyrrteyeussldlddkcjchccccccchdsfjdsjjeuedhjdkaldldl"

## Example

> table(rle(strsplit(x5,"")[[1]]))

## Output

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

## Example 6

Live Demo

> x6<-c("hryrhhdjdakadldldlaldaldldlldddddlkdfjdjjdkdkeepaotirueuwkslsdesssldldkeie")
> x6

## Output

[1] "hryrhhdjdakadldldlaldaldldlldddddlkdfjdjjdkdkeepaotirueuwkslsdesssldldkeie"

## Example

> table(rle(strsplit(x6,"")[[1]]))

## Output

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

## Example 7

Live Demo

> x7<-c("qqooqooeoeorrppppptptoorororofflglgllhjjjjhkglllflllfsoososananbbbvccbbcbcbbcmmmawwwbbbzzzswwqqllooipp")
> x7

## Output

[1] "qqooqooeoeorrppppptptoorororofflglgllhjjjjhkglllflllfsoososananbbbvccbbcbcbbcmmmawwwbbbzzzswwqqllooipp"

## Example

> table(rle(strsplit(x7,"")[[1]]))

## Output

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

## Example 8

Live Demo

> x8<-c("EHEJAIDKDLLDLFAYWYWIWOLWLLLSLSLLSLSLSLSSLSPPPOOUTUJWWNNNSSNNANNDMQQLLLJJEPEPEPPPRPRPSPSPS")
> x8

## Output

[1] "EHEJAIDKDLLDLFAYWYWIWOLWLLLSLSLLSLSLSLSSLSPPPOOUTUJWWNNNSSNNANNDMQQLLLJJEPEPEPPPRPRPSPSPS"

## Example

> table(rle(strsplit(x8,"")[[1]]))

## Output

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

Updated on: 05-Jan-2021

415 Views