Discover how to effectively compare string sequences and count the gaps in R. Our step-by-step guide walks you through the solution with examples. --- This video is based on the question https://stackoverflow.com/q/70398747/ asked by the user 'Rdu U' ( https://stackoverflow.com/u/17704494/ ) and on the answer https://stackoverflow.com/a/70398833/ provided by the user 'Andre Wildberg' ( https://stackoverflow.com/u/9462095/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: R, compare strings and count Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- How to Count Gaps Between String Sequences in R String comparison can often be challenging, especially when you are dealing with biological sequences or any kind of data that requires precise alignment. In this post, we'll explore a common problem faced by R users – counting the gaps in a reference column based on a corresponding input sequence. The Problem: Counting Gaps in Sequences Imagine you have a data frame containing short sequences of DNA or similar data. Each sequence has a corresponding reference sequence, and you want to determine how many gaps (-) exist in the reference column for each sequence based only on the positions where there are actual alphabetic characters (A, C, T, G) in the input sequence. Sample Data Frame Let’s consider the following sample data frame: [[See Video to Reveal this Text or Code Snippet]] The columns represent the following: ip: Input sequences that might contain gaps ref: Reference sequences where gaps need to be counted gap: A placeholder for the resulting gap counts The Challenge The requirement is to count gaps in the ref column only where there is a corresponding alphabetic character in the ip column. For example: In the first row, ip is ATCGGGTTA and ref is AT--GATCT. The gap count should be 2 (for the -- in the second and third positions). In the second row, with ip as AT--GATCT, there should be a gap count of 0 since AT--GATCT does not have any corresponding characters matching gaps in the reference. The third row would register 1 gap. The Initial Attempt The initial approach attempted the following code: [[See Video to Reveal this Text or Code Snippet]] However, this produced misleading results, returning 2 for the third row instead of the expected 1. The Solution: A Refined Approach To accurately count the gaps, we need a more systematic approach. Here’s the modified code that achieves just that: [[See Video to Reveal this Text or Code Snippet]] Breakdown of the Solution Using mapply: This function allows us to apply a function to each element of the input vectors (in this case, the split characters of ip and ref). Checking for Gaps: The logic grepl("-", y) returns a logical vector where gaps in the ref are present. Simultaneously, grepl("-", x) checks for gaps in ip. Combining Conditions: By using logical conditions, we ensure that we only count a gap in ref when there is a corresponding alphabetic character in ip. Summing Up: colSums aggregates the results across columns to output the total count of gaps for each entry. The Final Data Frame After applying the above logic, the data frame will display the correct number of gaps: [[See Video to Reveal this Text or Code Snippet]] Conclusion Counting gaps in sequences based on conditions is a common task in data analysis in R, particularly in bioinformatics. By refining our approach, we ensure accurate results that align with the expectations of sequence comparison tasks. If you're working with larger datasets, be sure to test this solution comprehensively to account for any edge cases. Happy coding in R!