of course I could replace specific arguments like this:
mydata=c("á","é","ó")
mydata=gsub("á","a",mydata)
mydata=gsub("é","e",mydata)
mydata=gsub("ó","o",mydata)
mydata
but surely there is a easier way to do this all in onle line, right? I dont find the gsub help to be very comprehensive on this.
Related to Justin's answer:
And you can get rid of the names with
names(*) <- NULL
if you want.You can use the
match
function. Herematch(x, y)
returns the index ofy
where the element ofx
is matched. Then you can use the returned indices, to subset another vector (sayz
) that contains the replacements for the values ofx
, appropriately matched withy
. In your case:In a simpler example, consider the situation below, where I was trying to substitute
a
for'alpha'
,'b'
for'beta'
and so forth.An interesting question! I think the simplest option is to devise a special function, something like a "multi" gsub():
Which gives me:
Use the character translation function