I have a data.frame, df,
df=data.frame(col1=1:5, col2=c("human","worm","worm","yeast","human"))
in which column "col2", has strings such as "yeast", "human","worm" and I want to replace these with "sce", "hsa", "cel". How can I do this?
I could do
df[,idx]=lapply(df[,idx],function(x){ gsub(oldname,newname,x) })
but this solution only works one at a time but I am seeking to do it all in one go, like a translate table like so
df[,idx]=lapply(df[,idx],function(x){ gsub(c(oldname1,oldname2), c(newname1,newname2),x) })
Thanks