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.
Hi: Does this work for you?> x <- sample(c('AB', 'ABC', 'ABCD', 'East', 'West', 'North', 'South'),+ 100, replace = TRUE)> table(x)x AB ABC ABCD East North South West 14 11 15 14 13 16 17> x2 <- ifelse(x %in% c('East', 'West', 'North', 'South'), strtrim(x, 1), x) > table(x2)x2 AB ABC ABCD E N S W 14 11 15 14 13 16 17 HTH, Dennis On Wed, Apr 14, 2010 at 3:11 PM, Chuck <vijay.nori@gmail.com> 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. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
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.