An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090328/9945b5ef/attachment-0002.pl>
text matching and substitution
4 messages · simeon duckworth, 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","other") -
irrespective of case
its a large dataset, so i'd like to be able to do this as efficiently as
possible.
thanks for any help
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090328/849b5caf/attachment-0002.pl>
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 duckworth schrieb:
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
really inefficient way of doing something quite simple anyhow.
colours <- as.character(paste(letters,colours(),"stuff",LETTERS))
target <- c("red","blue","green","gray")
new.colour <-colours
for (i in length(target)) {
x <- target[i]
new.colour[grep((x),new.colour)] <- x
return(new.colour)
}
On Sat, Mar 28, 2009 at 9:45 AM, Stephan Kolassa <Stephan.Kolassa at gmx.de>wrote:
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","other") -
irrespective of case
its a large dataset, so i'd like to be able to do this as efficiently as
possible.
thanks for any help
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.