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.
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With