Sunday 15 July 2012

r - How to do pairwise replacement of strings efficiently -


I have a list of coefficient names and I want to change the technical terms with good words. A function call will replace

  (list ("coffee1", "coffee2"), coffee1 = "value", coffee2 = "eventdemi", cof3 = "income" )   

and

  "value" "Events"    

It is quite easy. Store your translations in the named vector:

  Translation & lt; -c (coef1 = "price", coef2 = "eventdummy", coef3 = "earnings")   

then [:

 < Code> vector.of.strings & lt; - c ("coef1", "coef2") translation [vector.of.strings] Use # Coef1 coef2 # "price" "event dummy"   

As you can see , The output is also a named vector if the name is a problem for you, then you can just do unname (translations [vector.of.strings]) .

In addition, if your original name is in a list, your example, you already have unlist :

  translations [unlist (list. Of.strings)]  Important:  All the above work well if all your original names have a replacement. If this is not the case and you just want to modify the names for which the replacement is, you can do this:  
  ifelse (is.na (translations [vector.of.strings ]), Vector.ofstring, translation [vector.ofstring])    

No comments:

Post a Comment