Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count frequencies of certain character in a string?

Tags:

string

regex

r

If I have a run of characters such as "AABBABBBAAAABBAAAABBBAABBBBABABB".

Is there a way to get R to count the runs of A and state how many of each length ?

So I'd like to know how many instances of 3 A's in a row, how many instances of a single A, how many instances of 2 A's in a row, etc.

like image 464
user3754366 Avatar asked Jun 24 '15 09:06

user3754366


People also ask

How do you count the frequency of a character in a string?

You can use a java Map and map a char to an int . You can then iterate over the characters in the string and check if they have been added to the map, if they have, you can then increment its value. At the end you will have a count of all the characters you encountered and you can extract their frequencies from that.

How do you count the frequency of a character in a string in python?

First, create a string variable, accept the string from the user and store it into a variable. In this case, str1 is a string variable. Next, create an empty dictionary to store the character as key and frequency of character as value. Loop over the string to get one character at a time (each iteration).

How do you count the frequency of each character in a string C++?

The code snippet for this is given as follows. for(int i = 0; str[i] != '\0'; i++) { if(str[i] == c) count++; } cout<<"Frequency of alphabet "<<c<<" in the string is "<<count; The program to find the frequency of all the alphabets in the string is given as follows.

How do you find the frequency of a letter in a string in C?

Iterate over the given string S and increment the frequency of each character encountered by 1, by performing freq[S[i] – 'a'] += 1. If S[i] = 'a', then S[i] – 'a' is equal to 0, therefore the frequency of 'a' is incremented in the array.


1 Answers

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

gives

       values
lengths A B
      1 3 1
      2 2 3
      3 0 2
      4 2 1

which (reading down the A column) means there were 3 A runs of length 1, 2 A runs of length 2 and 2 A runs of length 4.

like image 170
Glen_b Avatar answered Sep 28 '22 04:09

Glen_b