[R] replace a value in a vector

Thomas Lumley tlumley at u.washington.edu
Thu Oct 7 23:52:42 CEST 2004

On Thu, 7 Oct 2004, Paul Lepp wrote:

> OK, so this is a really stupid question and should be incredibly simple to
> do but I can't figure it out. So maybe someone would be so kind as to tell
> me.
> I have a vector of zeros and ones.  I want to replace all of the zeros with
> "black" and all of the ones with "gray".  That's it.  Any help would be
> appreciated.

You can do this with

yournewvector <-   ifelse(yourvector == 1, "black", "gray")

but you may actually want to create a factor

yournewvector <- factor(youroldvector,  labels=c("gray", "black"))


More information about the R-help mailing list