Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: How to count length of intervals between specific word/symbol in a vector?

I have a vector that contains series of texts and numbers, like:

t <- c("A", 1:3, "A", 1:4, "A", 1:3)
t
#>  [1] "A" "1" "2" "3" "A" "1" "2" "3" "4" "A" "1" "2" "3"

Created on 2022-08-06 by the reprex package (v2.0.1)

That is, the actual data is taken from a pdf, with the data frame collapsed into a single column vector, and the wrap length is uneven for some reason (probably because of the cell merging).
To process this data efficiently, I want to know the length from "A" to next "A" or end. In this example the answer would be 3, 4, 3 (Edit: sorry for a simple mistake, it would be 4, 5, 4).
I have tried many different methods but can’t find one that works. Does anyone know of a better way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want the number of elements

  • (1) between adjacent "A"s;
  • (2) from the last "A" (excluding it) to the end.

We can use either of the following:

diff(c(which(t == "A"), length(t) + 1)) - 1
#[1] 3 4 3

diff(which(c(t, "A") == "A")) - 1
#[1] 3 4 3

Essentially we pad an "A" at the end to turn (2) into (1). If the last element of t happens to be an "A", the last value in the result will be 0.


Extension:

If you further want to know the number of elements from the beginning to the first "A" (excluding it), we can pad a leading "A":

diff(c(0, which(t == "A"), length(t) + 1)) - 1
#[1] 0 3 4 3

diff(which(c("A", t, "A") == "A")) - 1
#[1] 0 3 4 3

Here, the first value is 0, because the first element of t happens to be an "A".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading