[R] Search and extract string function
AndrewPage
savejarvis at yahoo.com
Thu Jul 15 16:48:59 CEST 2010
Hi all,
I'm trying to write a function that will search and extract from a long
character string, but with a twist: I want to use the characters before and
the characters after what I want to extract as reference points. For
example, say I'm working with data entries that looks like this:
Drink=Coffee:Location=Office:Time=Morning:Market=Flat
Drink=Water:Location=Office:Time=Afternoon:Market=Up
Drink=Water:Location=Gym:Time=Evening:Market=Closed
Drink=Wine:Location=Restaurant:Time=LateEvening:Market=Closed
...
For my function, I'd like to find what's located between "Location=", and
":Time=" in every instance, and extract it, to return something like
"Office, Office, Gym, Restaurant".
In a previous discussion I found
(http://tolstoy.newcastle.edu.au/R/help/05/03/0344.html), someone wrote a
function where you could find and substitute characters in a string, based
on "pre" and "post" variables:
interp <- function(x, e = parent.frame(), pre = "\\$", post = "" ) {
for(el in ls(e)) {
tag <- paste(pre, el, post, sep = "")
if (length(grep(tag, x))) x <- gsub(tag, eval(parse(text = el), e), x)
}
x
}
I'm not sure how to modify it, however, to do what I want it to do. Any
suggestions?
Thanks in advance,
Andrew
--
View this message in context: http://r.789695.n4.nabble.com/Search-and-extract-string-function-tp2290268p2290268.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list