Lee Sande
Lee Sande

Reputation: 467

how to replace multiple values in a specific row in a data.frame?

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

Upvotes: 0

Views: 875

Answers (3)

akrun
akrun

Reputation: 886938

We can use match

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

Upvotes: 1

user31264
user31264

Reputation: 6727

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

Upvotes: 3

PoisonAlien
PoisonAlien

Reputation: 431

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

Upvotes: 1

Related Questions