Reputation: 13
I'm from pharmaceutical background.I'm new to coding. I've started reading the book "The art of R programming!" I'm getting the concepts of it by time. Well, I'm stuck on the topic named "Finding Runs of Consecutive Ones." In the particular example There is vector (1,0,0,1,1,1,0,1,1)from which the number of 1s are to be find. If we want to find consecutive two 1s the code should return the value (4,5,8) I'm unable to understand the code described in the book.
findruns <- function(x,k) {
n <- length(x)
runs <- NULL
for (i in 1:(n-k+1)) {
if (all(x[i:(i+k-1)]==1)) runs <- c(runs,i)
}
return(runs)
}
I have list of questions.. 1. What does this x and k mean in the 1st line. 2. Why we need to assign NULL to runs 3. What does line 4 and 5 exactly mean!
PS: I know my questions are way too easy to answer as its from initial part of the book!
Upvotes: 1
Views: 1106
Reputation: 886948
We could get the index of consecutive tuple of ones, using stri_locate_all
. We paste
the vector
'v1' to a single string, and use the regex lookaround ((?=11)
) to match the pattern. The stri_locate_all
gives the 'start' and 'end' index of all those tuples of 11
. Here I extracted only the start
column ([,1]
)
library(stringi)
stri_locate_all(paste(v1, collapse=""), regex="(?=11)")[[1]][,1]
#[1] 4 5 8
Regarding the OP's function, it has two input variables, 'x' and 'k' where 'x' represents the vector ('v1'), 'k' the tuple length which I guess would be 2. We assign 'n' as the length
of the vector, create a NULL
vector 'runs' for allocating the output index. Then, we loop through the sequence of the vector until the 6th element (n-k+1
), and for each 'i', we again take the sequence beginning from 'i' to i+k-1
i.e. if the 'i' is 1, the index will be '2', and the sequence is 1:2
, get the elements in the vector corresponds to that v1[1:2]
, check whether it is equal to 1, if all
the elements are 1, then we concatenate the 'runs' with the corresponding index ('i')
v1 <- c(1,0,0,1,1,1,0,1,1)
Upvotes: 2