0

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

3
  • Please include sample data Commented Dec 25, 2016 at 1:40
  • this might be of help: stackoverflow.com/questions/7547597/… Commented Dec 25, 2016 at 1:43
  • @Stedy Thank you. That was exactly what I was looking for: Commented Dec 25, 2016 at 1:51

3 Answers 3

3
df=data.frame(col1=1:5, col2=c("human","worm","worm","yeast","human"))
a=c(yeast="sce",human="hsa",worm="cel")
df$col2 = a[df$col2]

Result:

> df
  col1 col2
1    1  sce
2    2  hsa
3    3  hsa
4    4  cel
5    5  sce
Sign up to request clarification or add additional context in comments.

Comments

1

Try using factors.

df=data.frame(col1=1:5, col2=c("human","worm","worm","yeast","human"))
    > df
      col1  col2
    1    1 human
    2    2  worm
    3    3  worm
    4    4 yeast
    5    5 human

    > df$col2 = as.character(factor(x = df$col2, levels = c('yeast', 'human', 'worm'), labels = c('sce', 'hsa', 'cel')))
    > df
      col1 col2
    1    1  hsa
    2    2  cel
    3    3  cel
    4    4  sce
    5    5  hsa

Comments

1

We can use match

library(dplyr)
df %>%
   mutate(col2 = a[match(col2, names(a))])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.