Note that "male" comes before "female" in your data frame.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

syrvn <mentor_@gmx.net> wrote:

Hi,

thanks for the tip! I do it as follows now but I still have a problem I do
not understand:


abbrvs <- data.frame(c("peter", "name", "male", "female"),
		 	 c("P", "N", "m", "f"))
						
colnames(abbrvs) <- c("pattern", "replacement")
	
str <- "My name is peter and I am male"

for(m in 1:nrow(abbrvs)) {
		str <- sub(abbrvs$pattern[m], abbrvs$replacement[m], str, fixed=TRUE)
		print(str)
	}
	

This works perfectly fine as I get: "My N is P and I am m"

However, when I replace male by female then I get the following: "My N is P
and I am fem"

but I want to have "My N is P and I am f".

Even with the parameter fixed=true I get the same result. Why is that?




--
View this message in context: http://r.789695.n4.nabble.com/Which-function-to-use-grep-replace-substr-etc-tp3909871p3909922.html
Sent from the R help mailing list archive at Nabble.com.

_____________________________________________

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]]

