Re: Minutes and Hours

John Wallace (jrw@fish.washington.edu)
Wed, 28 Jan 1998 15:20:30 -0800 (PST)


On Wed, 28 Jan 1998, Catherine Collins wrote:

> On a related topic, can S-Plus unpack expressions like "5:30:05 AM"? Has
> anybody written anything to do this?
>

get.subs("5:30:05 AM", sep=c(":", " "))
[1] "5" "30" "05" "AM"

"get.subs" <- function(x, sep = ",")
{
# DATE WRITTEN: 26 May 1995, Revised 28 Feb. 1996
# Author: John R. Wallace (jw@u.washington.edu)
#
if(length(sep) == 1) sep <- substring(sep, 1:nchar(sep), 1:nchar(sep))
nc <- nchar(x)
y <- (1:nc)[is.element(substring(x, 1:nc, 1:nc), sep)]
if(is.na(y[1] + 0))
return(x)
substring(x, c(1, y + 1), c(y - 1, nc))
}

is.element() comes with V4, but for V3.X users:

"is.element" <- function(el, set)
!is.na(match(el, set, nomatch = NA))

-- 
John Wallace
University of Washington                    ^    ^    ^
Fisheries Research Institute               / \  / \  / \   ^
Box 357980                                 / \  / \   |   / \
Seattle, WA 98195-7980                      |    |  o__~  / \
PHONE   (206) 543-1513                  @ @         /\/\   |
FAX     (206) 685-7471                   ~    
E-MAIL        jw@u.washington.edu              o
WWW      http://www.fish.washington.edu/people/jrw/Wallace.html 
                                                o  _///_ //
                                                <`)=  _<<
                                                    \\\  \\