Re: [R] text matching and substitution

2009-03-28 Thread Wacek Kusnierczyk
Stephan Kolassa wrote: > Hi Simeon, > > I played around a little with Vectorize and mapply, but I couldn't > make it work :-( So, my best guess would be a simple loop like this: > > result <- as.character(paste(letters,colours(),"stuff",LETTERS)) > target <- c("red","blue","green","gray") > for ( n

Re: [R] text matching and substitution

2009-03-28 Thread baptiste auguie
yet another attempt, colours <- as.character(paste(letters,colours(),"stuff",LETTERS)) target <- c("red","blue","green","gray") matches <- melt(sapply(target, grep, x=colours)) colours[matches$value] <- matches$L1 (probably a worse idea than a straight for loop, though) baptiste On 2

Re: [R] text matching and substitution

2009-03-28 Thread simeon duckworth
Jim/Stephan - absolutely perfect. thank you. very grateful for your help. simeon On Sat, Mar 28, 2009 at 5:27 PM, jim holtman wrote: > Does this do what you want: > > > x <- c('xxxredxxx', 'blue', 'xx', 'greenbluered') > > pat <- 'red|green|blue' > > result <- sub(paste("^.*?(",

Re: [R] text matching and substitution

2009-03-28 Thread jim holtman
Does this do what you want: > x <- c('xxxredxxx', 'blue', 'xx', 'greenbluered') > pat <- 'red|green|blue' > result <- sub(paste("^.*?(", pat, ").*", sep=""), "\\1", x) > # check if no match in original string; replace with 'other' > match <- grep(pat, x) > result[-match] <- 'other' > r

Re: [R] text matching and substitution

2009-03-28 Thread Stephan Kolassa
Hi Simeon, I played around a little with Vectorize and mapply, but I couldn't make it work :-( So, my best guess would be a simple loop like this: result <- as.character(paste(letters,colours(),"stuff",LETTERS)) target <- c("red","blue","green","gray") for ( new.color in target ) { result[grep

Re: [R] text matching and substitution

2009-03-28 Thread Stephan Kolassa
Hi Simeon, I'm slightly unclear on what exactly you are trying to achieve... Are you trying to replace every entry of colours which *contains* "red" by "red", dropping the rest of the entry? And same with "blue"? A short example "before & after" would be helpful... Best, Stephan simeon duc

Re: [R] text matching and substitution

2009-03-28 Thread simeon duckworth
thanks stephan. i'd been trying to make gsub work, but couldnt make it replace the whole expression. so i'd resorted to trying to loop with grep - but with two problems. firstly, i cant seem to make the loop 'remember' the substitutions it makes (see below). secondly, it feels like this is a r

Re: [R] text matching and substitution

2009-03-28 Thread Stephan Kolassa
Hi Simeon, ?gsub HTH, Stephan simeon duckworth schrieb: I am trying to simplify a text variable by matching and replacing it with a string in another vector so for example in colours <- paste(letters,colours(),"stuff",LETTERS) find and replace with ("red","blue","green","gray","yellow","othe

[R] text matching and substitution

2009-03-28 Thread simeon duckworth
I am trying to simplify a text variable by matching and replacing it with a string in another vector so for example in colours <- paste(letters,colours(),"stuff",LETTERS) find and replace with ("red","blue","green","gray","yellow","other") - irrespective of case its a large dataset, so i'd like