[R] search and replace

Chuck vijay.nori at gmail.com
Thu Apr 15 00:11:14 CEST 2010


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.



More information about the R-help mailing list