[R] help with replacing factors

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 24 14:12:25 CEST 2009


Try storing them as character strings rather than factors:

black_gray <- data.frame(black, gray, stringsAsFactors = FALSE)

Try this to view what you've got:

str(black_gray)

On Sun, May 24, 2009 at 7:15 AM, Andreas Christoffersen
<achristoffersen at gmail.com> wrote:
> Hi,
>
> In the example dataset below - how can I cahnge "gray20", to "blue"
>
> # data
> black <- rep(c("black","red"),10)
> gray <- rep(c("gray10","gray20"),10)
> black_gray <- data.frame(black,gray)
>
> # none of this desperate things works
> # replace(black_gray$gray, gray=="gray20","red")
> # if(black_gray$gray=="gray20"){black_gray$gray<-"blue"}
> # for (i in
> black_gray$gray)if(black_gray$gray[i]=="gray20"){black_gray$gray[i]
> <-"blue"}
> # black_gray$gray=="gray14" <- "blue"
> # black_gray$gray[gray=="gray20"] <- "blue"
> # subset(black_gray,gray=="gray20",gray) <-rep("blue",10)
>
> I have a feeling this is me misunderstanding some very basic stuf about the
> R engine... So any help will be much appreciated.
>
> Thanks in advance
>
> Andreas
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list