[R] Change factor levels

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Dec 14 21:30:37 CET 2013


On 14.12.2013 21:09, Gang Chen wrote:
> Suppose I have a dataframe 'd' defined as
>
>       L3 <- LETTERS[1:3]
>       d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
> = TRUE))
>       (d <- d0[d0$fac %in% c('A', 'B'),])
>
>    x y fac
> 2 1 2   B
> 3 1 3   A
> 4 1 4   A
> 5 1 5   A
> 6 1 6   B
> 8 1 8   A
>
> Even though factor 'fac' in 'd' only has 2 levels, but it seems to bear the
> birthmark of 3 levels from its parent 'd0':
>
> str(d)
>
> 'data.frame': 6 obs. of  3 variables:
>   $ x  : num  1 1 1 1 1 1
>   $ y  : num  2 3 4 5 6 8
>   $ fac: Factor w/ 3 levels "A","B","C": 2 1 1 1 2 1

d$fac <- factor(d$fac)
or
d$fac <- d$fac[,drop=TRUE]

Best,
Uwe Ligges

> How can I cut the umbilical cord so that factor 'fac' in 'd' would have an
> accurate birth certificate with the correct number of levels? Apparently
> the following does not work:
>
> levels(d$fac) <- c('A', 'B')
>
> Also any reason for this heritage?
>
> Thanks,
> Gang
>
> 	[[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