Knowledge base

Replacement of multiple contents of vector in R


Somethimes we need to replace multiple contents of vector in R , like I want to replace a to c and b to d in x vector . Obviously you can use gsub to finish it by two times : gsub (a , c , x ) ; gsub (b , d , x ) . Actually, we can do it in a more readable way .

Code exmaple

> library(plyr)
> x <- c("a", "b", "f")
> mapvalues(x, c("a", "b"),c("c", "d"))
[1] "c" "d" "f"

Here the old names were changed into new names at one time .


The manual of mapvalues function




Leave a Reply

Your email address will not be published. Required fields are marked *