s-news
[Top] [All Lists]

[S] split.string, substring.location etc

To: s-news@wubios.wustl.edu
Subject: [S] split.string, substring.location etc
From: Christian Hoffmann <christian.hoffmann@wsl.ch>
Date: Fri, 25 Feb 2000 14:53:51 +0100
Sender: owner-s-news@wubios.wustl.edu
Hi all,

Thanks to input from

Cesare Furlanello -- IRST, May 16 1997
Christian Keller <ckeller@aicos.com>
Thomas Lumley <thomas@biostat.washington.edu>

I was able to solve my problem of split.str =. strsplit:

split.string <- function(string,pattern,clean=F){
  pattern.loc <- substring.location(string,pattern)
  if (pattern.loc$first[1] > 0 ) {
    splits <- substring(string,
                        c(1, pattern.loc$last + 1), 
                        c(pattern.loc$first-1, nchar(string)) 
                        )
  }
  else 
    splits <- string
  if(clean == T){
    splits <- splits[splits != ""]
  }
  return(splits)
}

substring.location <- function(text, string, restrict) {
  if(length(text)>1) stop('only works with a single character string')
  l.text <- nchar(text)
  l.string <- nchar(string)
  if(l.string > l.text) return(list(first=0,last=0))
  if(l.string==l.text)  return(if(text==string)list(first=1,last=l.text)
  else  list(first=0,last=0))
  is <- 1:(l.text-l.string+1)
  ss <- substring(text, is, is+l.string-1)
  k <- ss==string
  if(!any(k)) return(list(first=0,last=0))
  k <- is[k]
  if(!missing(restrict)) k <- k[k>=restrict[1] & k<=restrict[2]]
  if(length(k)==0) return(list(first=0,last=0))
  list(first=k, last=k+l.string-1)
}

------------------------------------------------------------------------------

For everyone to use are the following bits of code:

formula2string <- function(formula)  {
  ## take a formula and return the left and the right hand sides
  dput(formula,"xxxxformula")
  vec <- scan(file="xxxxformula",character())
##  remove("xxxxformula")  do not execute, it might not find the file
  str <- paste(vec, sep=" ", collapse="")
  tilde <- substring.location(str,"~")$first[1]
  list(left = substring(str,1,tilde-1), right = substring(str,tilde+1))
}

formula2term.names <- function(formula,side)  {
  ##  take a formula and return the names of the terms of the side hand side
  fstr <- formula2string(formula)
  split.string(unlist(fstr[names(fstr)==side], use.names=F),"+")
}

formula2Rterm.names <- function(formula)  {
  formula2term.names(formula,"right")
}

formula2string(a ~ b + c)  #  $left: "a"   $right: "b+c"
formula2string(~ b + c)    #  $left: ""    $right: "b+c"
formula2string(a ~ .)      #  $left: "a"   $right: "."

split.string(formula2string(a ~ b + c)$right,"+")  #   "b" "c"
split.string(formula2string(a ~ b + c)$left,"+")   #   "a"

formula2term.names(a ~ b + c,"left")   #   "a"
formula2term.names(~ b + c,"left")     #   ""
formula2term.names(a ~ b + c,"right")  #   "b" "c"

formula2Rterm.names(a ~ b + c)         #   "b" "c"

--------------------------------------------------------------------------

Any comments?

I hope I did not stamp on anyone's feet for publishing this stuff  :-)


Thanks for your help
--Christian
Christian W. Hoffmann
Mathematics and Statistical Computing
Landscape Modeling and Web Applications
Swiss Federal Institute for Forest, Snow and Landscape Research
CH-8903 Birmensdorf, Switzerland
phone: ++41-1-739 22 77    fax  : ++41-1-737 40 80 or  ++41-1-739 2215  e-mail: 
Hoffmann@WSL.CH

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] split.string, substring.location etc, Christian Hoffmann <=