R/close.bracket.R

Defines functions close.bracket

close.bracket<-function(pattern, x){

  possible.end<-nchar(x)+1
  start<-gregexpr(pattern, x)[[1]]
  n<-length(start)
  openB<-gregexpr("\\(", x)[[1]]
  closeB<-gregexpr("\\)", x)[[1]]
  end<-1:n

  if(n==1 & start[1]==-1){
    end<--1
  }else{  
    for(i in 1:n){
      nopen<-1
      pos<-openB[match(TRUE, openB>start[i])]+1
      while(nopen!=0){
        if(pos%in%openB){nopen<-nopen+1}
        if(pos%in%closeB){nopen<-nopen-1}
        pos<-pos+1
        if(pos>possible.end){stop("formuala invalid")}
      }
      end[i]<-pos-1
    }
  }  
  cbind(start,end)
}

Try the MCMCglmm package in your browser

Any scripts or data that you put into this service are public.

MCMCglmm documentation built on May 2, 2019, 2:51 p.m.