[R] search and replace

Erik Iverson eriki at ccbr.umn.edu
Thu Apr 15 06:08:12 CEST 2010


Chuck wrote:
> I have a dataframe with almost a million rows which has one column
> with strings. That column has several entries with the words "South",
> "North", "East" and "West" which I would like to replace with S, N, E,
> and W, respectively. Obviously, I can use gsub multiple times   df
> $col2 <- gsub("West", "W", df$col2)  which will require multiple scans
> of the column.
> 
> Is there a succinct approach where all the replacements can be done
> with a single scan? Thanks.

You don't give a toy example.  If they are factors, just see the ?levels 
function, including its replacement version.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list